MaterialDesignInXamlToolkit对话失去焦点

时间:2016-07-12 15:13:41

标签: c# wpf xaml material-design-in-xaml

我正在使用MaterialDesignInXamlToolkit,我有一个在执行某些线程方法时失去焦点的Dialog。

  1. 首先,我向用户显示一条消息(效果很好);
  2. 然后,它启动另一个带进度环的对话框(效果很好);
  3. 最后,如果我们从服务器收到错误响应,则弹出另一个对话框并显示错误消息: No focus
  4. 问题是:“确定”按钮被禁用。只需点击一次即可获得焦点。

    这是一个源代码: https://drive.google.com/open?id=0B7dlCQ9y_LsDdm1mU2RNNzlXem8

    这是来自MainWindow.xaml的XAML:

    <Window x:Class="WpfExample.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:local="clr-namespace:WpfExample"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <materialDesign:DialogHost Identifier="RootDialog">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Button x:Name="button" Content="Button" Margin="0" Width="88" Height="31" Click="button_Click" Grid.Row="1"/>
    
        </Grid>
    </materialDesign:DialogHost>
    

    MainWindow.xaml.cs:

    using MaterialDesignThemes.Wpf;
    using System.Threading.Tasks;
    using System.Windows;
    using WpfExample.Dialogs;
    
    namespace WpfExample
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void button_Click(object sender, RoutedEventArgs e)
        {
            // Asks for confirmation
            BeforeRemoveItem();
        }
    
        private string RemoveItem()
        {
            /// In my project I'm calling a DELETE on a Rest Server 
            /// and if an error ocurred, we return the error message...
    
            // "Simulates" a delay from server..
            // PS: Without this, the "OK" dialog works fine.
            System.Threading.Thread.Sleep(3000);
    
            // Fake result
            return "An error has ocurred in Server";
    
        }
    
        private async void BeforeRemoveItem()
        {
            var messageDialog = new DialogQuestion
            {
                Message = {Text = "Do you want remove this item?"}
            };
    
            var result = await DialogHost.Show(messageDialog, "RootDialog", OnSelectAnswer);
    
        }
    
        private void OnSelectAnswer(object sender, DialogClosingEventArgs eventArgs)
        {
            if ((bool)eventArgs.Parameter == false)
                return;
    
            // Abort current process...
            eventArgs.Cancel();
    
            // Creates a new dialog, to show the progress bar...
            DialogProgress progresso = new DialogProgress();
            eventArgs.Session.UpdateContent(progresso);
    
            // Then starts a task..
            var t = Task<string>.Run(() =>
            {
                // Calls a methdo from server...
                return RemoveItem();
            })
            .ContinueWith(resultFromServer =>
            {
                // Now, with the answer from server we can do extra things..
    
                // If there is no message, we just close the current Message
                if (resultFromServer.Result == string.Empty)
                {
                    eventArgs.Session.Close(false);
                }
                else
                {
                    // If there is an error message, we show a simple dialog (with just an "OK" button) and shows the error..
                    eventArgs.Cancel();
                    eventArgs.Session.UpdateContent(new DialogOk { Message = { Text = resultFromServer.Result } });
    
                }
            }, TaskScheduler.FromCurrentSynchronizationContext() );
        }
    
       }
    }
    

0 个答案:

没有答案
相关问题