WPF现代界面在单击“链接”后弹出现代对话框

3
我正在使用WPF Modern UI,试图在单击链接后弹出ModernDialog。问题是链接只有Source选项,我不想导航到另一个页面(只想弹出ModernDialog)。我发现这个链接:如何在Modern UI wpf中单击菜单链接打开新窗口?但它只在第一次单击时弹出窗口,并且还导航到第一个选项卡页(我的意思是到我查看的页面的“父级”)。
有人有想法吗?这可行吗?
这是我的代码(相关部分),我说的是“connect”链接:
 <mui:ModernWindow.TitleLinks>
        <mui:Link x:Name="connect" DisplayName="connect"/>
        <mui:Link DisplayName="settings" Source="/Pages/SettingsPage.xaml" />
        <mui:Link DisplayName="help" Source="https://github.com" />
    </mui:ModernWindow.TitleLinks>
2个回答

2

您可以重写ModernWindowOnApplyTemplate()方法,并向ModernFrame添加NavigateLink命令绑定。如果您不设置LinkSource属性,则命令参数将为null

public partial class MainWindow : ModernWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var frame = Template.FindName("ContentFrame", this) as ModernFrame;
        if(frame != null)
            frame.CommandBindings.Add(new CommandBinding(FirstFloor.ModernUI.Windows.Navigation.LinkCommands.NavigateLink, OnNavigateLinkExecuted));
    }

    private void OnNavigateLinkExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        if(e.Parameter == null)
        {
            ModernDialog dialog = new ModernDialog();
            dialog.ShowDialog();
        }
        else
        {
            OnNavigateLink(sender, e);
        }
    }

    private void OnNavigateLink(object sender, ExecutedRoutedEventArgs e)
    {
        if (LinkNavigator != null)
        {
            Uri uri;
            string parameter;
            string targetName;

            if (FirstFloor.ModernUI.Windows.Navigation.NavigationHelper.TryParseUriWithParameters(e.Parameter, out uri, out parameter, out targetName))
                LinkNavigator.Navigate(uri, e.Source as FrameworkElement, parameter);
        }
    }
}

XAML:

<mui:ModernWindow.TitleLinks>
    <mui:Link x:Name="connect" DisplayName="connect"/>
    <mui:Link DisplayName="settings" Source="/Pages/SettingsPage.xaml" />
    <mui:Link DisplayName="help" Source="https://github.com" />
</mui:ModernWindow.TitleLinks>

1
我会采取不同的方法。
当您的MainWindow加载时,找到ModernFrame并挂钩导航事件。
在Navigating事件处理程序中,检查源是否以“dialog:”(任意字符串,使用任何您想要的)开头,然后显示对话框并取消导航。
链接的源设置为“dialog:Test”,表示我要显示“Test”对话框。

MainWindow.xaml

<mui:ModernWindow x:Class="WpfApp17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:mui="http://firstfloorsoftware.com/ModernUI"
        xmlns:local="clr-namespace:WpfApp17"
        mc:Ignorable="d"
        Loaded="ModernWindow_Loaded"
        ContentSource="/UserControl1.xaml"
        Title="MainWindow" Height="350" Width="525">
    <mui:ModernWindow.TitleLinks>
        <mui:Link x:Name="connect" DisplayName="connect" Source="dialog:Test" />
    </mui:ModernWindow.TitleLinks>
</mui:ModernWindow>

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using FirstFloor.ModernUI.Windows.Controls;

namespace WpfApp17
{
    public partial class MainWindow : ModernWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ModernWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var frame = VisualTreeHelperFindChildren<ModernFrame>(this).FirstOrDefault();
            if (frame != null)
                frame.Navigating += Frame_Navigating;
        }

        private void Frame_Navigating(object sender, FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            string dialog = "dialog:";
            if (e.Source.OriginalString.StartsWith(dialog))
            {
                // Show dialog
                var dialogName = e.Source.OriginalString.Remove(0, dialog.Length);
                MessageBox.Show($"Show Dialog '{dialogName}'");
                e.Cancel = true;
            }
        }

        public static List<T> VisualTreeHelperFindChildren<T>(DependencyObject parent) where T : class
        {
            List<T> list = new List<T>();

            if (parent != null)
            {
                int count = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < count; i++)
                {
                    // Get object at index i
                    DependencyObject dobj = VisualTreeHelper.GetChild(parent, i);

                    if (dobj is T)
                    {
                        list.Add(dobj as T);
                    }

                    // Loop through its children
                    list.AddRange(VisualTreeHelperFindChildren<T>(dobj));
                }
            }
            return list;
        }

    }
}

顺便提一下,UserControl1.xaml上只有一个文本块,没有其他内容。我添加它是为了让屏幕上有些东西。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接