英文:
WinUI 3 Create a Modal Search Form
问题
我正在研究将Windows Forms应用程序移植到WinUI 3。该应用程序具有一个模态搜索窗体,用于多个地方,如下图所示。
希望在WinUI 3中复制类似的功能,但尚未找到方法。
搜索窗体用于将访客实体分配给各种事件类型。当双击所需行时,所选访客将通过委托传回给调用窗体。
搜索窗体需要代码,而ContentDialog似乎不支持,因此不是选项。
我找不到在WinUI 3中创建类似功能的模态窗体/窗口的任何方法。这在业务应用程序中很常见,人们是如何在WPF中处理的呢?我对Xaml不熟悉,也想知道是否有另一种常用的设计模式,而不是使用模态搜索窗体。
英文:
I am looking into porting a Windows Forms application to WinUI 3. The application has a modal search form used in multiple places like the image below. This uses a modal form with code behind for flexible search logic, returning an object representing the selected item to the calling window.
I hope to reproduce something similar in WinUI 3, but have not found a way to do so.
The search form is used to assign Visitor entity to a variety of event types. The selected visitor is passed back to the calling form using delegates when the desired row is double clicked.
The search form requires code behind, and it looks like ContentDialog does not support that, so it is not an option.
I cannot find any way to create a modal form/window in WinUI 3 that would duplicate this functionality. Is it possible?
This is a common scenario in business applications, so how have people done this with WPF? I am new to Xaml, and am also wondering if there is another design pattern in common use instead of using a modal search form.
答案1
得分: 1
你可以使用 ContentDialog。
var yourPage = new Page();
var yourDialog = new ContentDialog()
{
XamlRoot = XamlRoot,
Content = yourPage
};
await yourDialog.ShowAsync();
MainPage.xaml:
<Button Click="ButtonBase_OnClick">打开 SearchPage</Button>
MainPage.xaml.cs:
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var yourPage = new SearchPage();
var yourDialog = new ContentDialog()
{
XamlRoot = XamlRoot,
Content = yourPage
};
yourPage.contentDialog = yourDialog;
await yourDialog.ShowAsync();
// 获取 SearchPage 的返回结果
var yourPageResult = yourPage.result;
}
SearchPage.xaml:
<Button Click="ButtonBase_OnClick">返回选定数据</Button>
SearchPage.xaml.cs:
public MyResult result { get; set; }
public ContentDialog contentDialog { get; set; }
public class MyResult
{
public long Id { get; set; }
public string Name { get; set; }
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
// 模拟搜索后的数据
result = new MyResult() { Id = 1, Name = "a" };
contentDialog.Hide();
}
英文:
You can use ContentDialog
var yourPage=new Page();
var yourDialog = new ContentDialog()
{
XamlRoot = XamlRoot,
Content = yourPage
};
await yourDialog.ShowAsync();
MainPage.xaml
<Button Click="ButtonBase_OnClick">Open SearchPage</Button>
MainPage.xaml.cs
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var yourPage = new SearchPage();
var yourDialog = new ContentDialog()
{
XamlRoot = XamlRoot,
Content = yourPage
};
yourPage.contentDialog = yourDialog;
await yourDialog.ShowAsync();
//Get the return result of SearchPage
var yourPageResult = yourPage.result;
}
SearchPage.xaml
<Button Click="ButtonBase_OnClick">Return Selected Data</Button>
SearchPage.xaml.cs
public MyResult result { get; set; }
public ContentDialog contentDialog { get; set; }
public class MyResult
{
public long Id { get; set; }
public string Name { get; set; }
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
//Data after simulated search
result = new MyResult() { Id = 1, Name = "a" };
contentDialog.Hide();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论