Caliburn.Micro + MaterialToolkitDialog无法打开..:(

时间:2018-10-12 12:43:28

标签: caliburn.micro

使用Calibrun.Micro(WPF) + MaterialToolkit, 我试图打开对话框。但不行 Could you check my code ? (in GitHub)

这里是小型测试项目。

点击“打开”按钮。

enter image description here

此按钮会触发这样的对话框。

public async void NewConnection(){
  var result = await DialogHost.Show(
  new UserControl { DataContext = new TestDialogViewModel() },
  "MainDialogHost");           
}

更改为灰色背景(好),但没有对话框。为什么? enter image description here

1 个答案:

答案 0 :(得分:1)

我找到了解决方法。

// MainViewModel.cs
TestDialogViewModel vm;
...
public async void NewConnection()
{

        vm = IoC.Get<TestDialogViewModel>();
        var dialog = new TestDialogView()
        {
            DataContext = vm
        };

        var result = await DialogHost.Show(dialog, "MainDialogHost",
            (object sender, DialogOpenedEventArgs eventArgs) =>
            {
                // pass DialogSession to ViewModel. 
                // View Model can close own dialog !
                vm.dialogSession = eventArgs.Session;
            },
            (object sender, DialogClosingEventArgs eventArgs) =>
            {
                Debug.WriteLine($"IPaddress = "+vm.IpAddress);                    
            });

    }

<!-- in MainView.xaml -->
<materialDesign:DialogHost Identifier="MainDialogHost" 
                           CloseOnClickAway="True"
                           cal:Message.Attach="[Event DialogClosing]=[Action DialogHost_OnDialogClosing()]"
                          >

和..

   // in TestDialogViewModel
   public DialogSession dialogSession;        
    public void BtnConnect()
    {
        dialogSession.Close();
    }

和..

<!-- in TestDialogView.xaml -->
<StackPanel Margin="20" Orientation="Vertical">
        <TextBlock>Input IP Address</TextBlock>
        <TextBox Margin="0 20 0 10" 
                 FontSize="28"
             HorizontalAlignment="Stretch" 
             Text="{Binding Path=IpAddress, Mode=TwoWay}"
                 TextAlignment="Center"/>
        <TextBlock Foreground="Red" TextAlignment="Center" HorizontalAlignment="Center"></TextBlock>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Style="{StaticResource MaterialDesignFlatButton}"
                    cal:Message.Attach="[Event Click]=[Action BtnConnect()]"
                        IsDefault="True"
                        Margin="0 8 8 0">
                <Button.CommandParameter>
                    <system:Boolean xmlns:system="clr-namespace:System;assembly=mscorlib">True</system:Boolean>
                </Button.CommandParameter>
                Connect
            </Button>
        </StackPanel>
    </StackPanel>

屏幕截图。

enter image description here

enter image description here

它将帮助Caliburn.Micro用户!

:)