WinUI 3 创建一个模态搜索表单

huangapple go评论50阅读模式
英文:

WinUI 3 Create a Modal Search Form

问题

我正在研究将Windows Forms应用程序移植到WinUI 3。该应用程序具有一个模态搜索窗体,用于多个地方,如下图所示。

希望在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.

WinUI 3 创建一个模态搜索表单

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

&lt;Button Click=&quot;ButtonBase_OnClick&quot;&gt;Open SearchPage&lt;/Button&gt;

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

&lt;Button Click=&quot;ButtonBase_OnClick&quot;&gt;Return Selected Data&lt;/Button&gt;

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 = &quot;a&quot; };
        contentDialog.Hide();
    }

huangapple
  • 本文由 发表于 2023年6月6日 12:17:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定