Blazor Hybrid Practical Experience: Pitfalls You Might Not Expect
Next, I will find time to try out the development experience with Electron and Wails, and further explore the advantages of Blazor Hybrid in desktop software development.
目录9 个章节
Foreword
I wrote an article yesterday introducing Blazor Hybrid technology, but due to length constraints, some issues were not explored in depth. Today, I would like to continue documenting several problems encountered while using Blazor Hybrid, as well as some current limitations of this technology.
Limitations of File Drag-and-Drop Events
The operating environment for Blazor Hybrid is WebView, which introduces certain limitations when handling file drag-and-drop. In traditional desktop applications (such as WinForms or WPF), developers can directly capture drag-and-drop events and obtain the full file path. However, in Blazor, drag-and-drop events can only be handled in the same way as in a browser, meaning we can only obtain the file stream for upload, but cannot retrieve the actual file path.
This creates significant inconvenience for features that require direct access to the file path (such as dragging a video into Clipify for processing).
Redundant Code (not really)
Students who have looked at the project code might notice that there is still code in FormMain.cs to handle DragDrop events, but it is not actually effective.
// Handle the drag enter event to detect if it is a file
private void blazorWebView1_DragEnter(object sender, DragEventArgs e) {
Console.WriteLine("drag enter");
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
// Change the mouse icon to indicate that dropping is allowed
e.Effect = DragDropEffects.Copy;
}
else {
e.Effect = DragDropEffects.None;
}
}
// Handle the drop event to get the file path
private void blazorWebView1_DragDrop(object sender, DragEventArgs e) {
Console.WriteLine("drag drop");
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
var files = (string[]?)e.Data.GetData(DataFormats.FileDrop);
// Here only a single file is handled, of course you can also handle multiple files
if (files?.Length > 0) {
var filePath = files[0]; // Get the path of the dropped file
MessageBox.Show($"File Path: {filePath}");
// Here you can pass the file path to Blazor or other processing logic
}
}
}
// Handle the DragOver event to prevent the system default behavior
private void blazorWebView1_DragOver(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect = DragDropEffects.Copy; // Explicitly allow file dropping
}
else {
e.Effect = DragDropEffects.None;
}
}
Solution
The current solutions are limited. Based on the research and my own exploration, there are a few approaches:
- Use a native WinForms control to cover the WebView when drag-and-drop is needed.
- Use hooking techniques to intercept the WebView's drag-and-drop events.
- Override the Blazor WebView control provided by Microsoft and implement the
WndProcmethod yourself.
The code for method 3 looks something like this (unverified):
public class CustomBlazorWebView : BlazorWebView {
protected override void WndProc(ref Message m) {
const int WM_DROPFILES = 0x233; // Drag-and-drop file message
if (m.Msg == WM_DROPFILES) {
// Handle file drag-and-drop logic here
// You can call your drag-and-drop event handling logic here
// Prevent message passing to avoid default system file handling
return;
}
base.WndProc(ref m);
}
}
PS: I found it too troublesome and haven't bothered to implement this drag-and-drop functionality yet. For now, I've only set up opening a dialog to select files.
Community Feedback
I've also seen many people raise similar issues on platforms like GitHub Issues and Stack Overflow, but it seems Microsoft isn't interested in addressing these problems.
Related Links:
- https://github.com/MicrosoftEdge/WebView2Feedback/issues/2805
- https://github.com/dotnet/maui/issues/2205
- https://stackoverflow.com/questions/73197778/net-maui-blazor-app-drag-and-drop-impossible
Desktop Application Experience Differences
Blazor Hybrid, although running as a desktop application, behaves more like a web application.
Browser Keyboard Shortcuts
A clear example is that pressing the F5 key in a WebView refreshes the page just like in a browser. This behavior is obviously inconsistent with the user experience of traditional desktop software.
In similar technologies like Electron, similar limitations exist. However, the difference is that Electron provides more control over browser behaviors, allowing developers to block or redefine these actions. Blazor Hybrid, on the other hand, currently lacks such fine-grained control capabilities.
From the perspective of desktop applications, users expect a consistent and native operating experience. Therefore, these subtle differences may influence developers' expectations for Blazor Hybrid applications.
Performance During Window Resizing
While using Blazor Hybrid, I also noticed issues with the smoothness of window resizing. Compared to native desktop applications, Blazor Hybrid's performance is not as satisfactory. When a user resizes the window, the interface occasionally exhibits black edges or screen tearing.
This issue is not limited to Blazor Hybrid. In fact, I've observed similar problems in browsers (Chrome) and Electron-based applications (QQ).
To better understand this phenomenon, I also tested a native C++ application and found that native apps are relatively smoother during window resizing, without the issues of black edges or tearing.
I speculate that the reason for this difference might be that Blazor Hybrid and Electron rely on WebView as their rendering engine. The rendering mechanism of WebView is less efficient than that of native UI rendering engines when handling window resizing.
Summary
Blazor Hybrid is a very promising technology that enables C# developers to easily build cross-platform desktop applications.
However, during my usage, I identified several issues that require attention, particularly regarding drag-and-drop events, consistency in desktop application behavior, and performance during window resizing.
These problems can currently cause some inconvenience for developers and also affect the smoothness of the user experience.
Next, I will find the time to try out the development experience with Electron and Wails to further explore the advantages of Blazor Hybrid in desktop software development.
_EOF_
Blazor Hybrid Practical Experience: Pitfalls You Might Not Expect
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
AOT编译Avalonia应用:StarBlog Publisher项目实践与挑战
本文介绍了作者使用Avalonia框架开发的跨平台文章发布工具StarBlog Publisher,并详细记录了使用AOT(Ahead-Of-Time)技术发布Avalonia应用的过程及遇到的问题。文章讨论了AOT的优缺点、配置方法,并针对JSON序列化、类型注册等问题提供了解决方案。作者分享了使用Newtonsoft.Json库和rd.xml文件等技巧,以优化AOT发布的性能和兼容性。
2025-04-27 · Desktop · 约 1 分钟
PC软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
国庆假期各种活动比较多,直到上班才有时间来更新文章~ 不过这两天我还是做了个小玩意(Clipify),起因是想给之前开发来自己用的简单视频剪辑工具 QuickCutSharp 加个功能,不过这个软件是基于 WinForms 开发的,做界面得拖拉控件,感觉繁琐又不灵活,于是索性重新做一个。
2025-03-27 · Desktop · 约 1 分钟
提升Avalonia UI质感,跨平台图标库选型实践
本文介绍了在Avalonia框架下使用两种图标库(Icon.Avalonia和IconPacks.Avalonia)的详细方法。作者通过对比不同设备间emoji显示不一致的问题,引出使用FontAwesome图标的优势。文章重点讲解了两种库的安装步骤、XAML配置方法及实际应用场景,包括独立图标显示、控件附加、动画效果实现等。其中IconPacks.Avalonia支持35种图标集,功能更为丰富,还提供了图标浏览器工具。全文通过具体代码示例,为开发者提供了实用的技术参考。
2025-06-12 · Desktop · 约 1 分钟
聊聊几个相见恨晚的WinForm界面库
前阵子在逛知乎的时候看到了Gitee发的文章,里面介绍了一些WinForm开源项目,其中就有涉及到界面库,我一下子就来了兴趣,然后顺瓜摸藤又找到了几个类似的界面库,看着熟悉的界面和代码,有一种「爷的青春回来了」的感觉~ 可惜属于WinForm的短暂时代早已结束,当今Web统治一切,怕是WinForm以后只能用来追忆往事了吧…… SunnyUI 看看作者的截图: 对话框: 看这界面颇有年代感,很惊讶
2025-03-27 · Desktop · 约 1 分钟
讨论
共 0 条加载评论中…