2017-06-05 20 views
0

私はCaliburn Microを使い始め、IResultの周りを頭で覆そうとしています。そうするために、私はいくつかのダミーコードを書いています。このコードは、テキストが消えるはずの長い操作(Task.Delay)が完了するまで、テキストボックスに「Loading ...」と表示されます。ここに私のコードは次のとおりです。Caliburn Micro - IResultとレンダリングビューを使用する

のViewModel:

[Export(typeof(IShell))] 
public class ShellViewModel : IShell 
{ 
    public string MyMessage { get; set; } 

    public IEnumerable<IResult> DoSomething() 
    { 
     yield return Loader.Show("Loading..."); 
     yield return Task.Delay(1000).AsResult(); 
     yield return Loader.Hide(); 
    } 
} 

ビュー:

<Window x:Class="CaliburnMicroTest.ShellView" 
     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:CaliburnMicroTest" 
     xmlns:cal="http://www.caliburnproject.org" 
     mc:Ignorable="d" 
     Title="ShellView" Height="300" Width="300"> 
    <StackPanel> 
     <Button Content="Do Something" 
       x:Name="DoSomething" /> 
     <TextBox Text="{Binding Path=MyMessage, Mode=TwoWay}"/> 
    </StackPanel> 
</Window> 

Loaderクラス:私はボタンをクリックすると

public class Loader : IResult 
{ 
    readonly string message; 
    readonly bool hide; 

    public Loader(string message) 
    { 
     this.message = message; 
    } 

    public Loader(bool hide) 
    { 
     this.hide = hide; 
    } 

    public event EventHandler<ResultCompletionEventArgs> Completed; 

    public void Execute(CoroutineExecutionContext context) 
    { 
     var target = context.Target as ShellViewModel; 

     target.MyMessage = hide ? string.Empty : message; 

     Completed(this, new ResultCompletionEventArgs()); 
    } 

    public static IResult Show(string message = null) 
    { 
     return new Loader(message); 
    } 

    public static IResult Hide() 
    { 
     return new Loader(true); 
    } 
} 

、私はテキストボックスを埋めることを期待します「Loading ...」を1秒間押してから再び空になりますが、テキストボックスには何も表示されません。また、私がデバッグすると、私のViewModelのMyMessageプロパティは "Loading ..."の値を持っています。なぜ私の見解にテキストが表示されませんでしたか?

あなたのビューモデルクラスは PropertyChangedBaseから継承し、変更通知を上げる必要がある
+0

役に立つ答えを投票することを忘れないでください:) https://stackoverflow.com/help/privileges/vote-up – mm8

答えて

1

[Export(typeof(IShell))] 
public class ShellViewModel : IShell, PropertyChangedBase 
{ 
    string _myMessage; 
    public string MyMessage 
    { 
     get { return _myMessage; } 
     set 
     { 
      _myMessage = value; 
      NotifyOfPropertyChange(() => MyMessage); 
     } 
    } 

    public IEnumerable<IResult> DoSomething() 
    { 
     yield return Loader.Show("Loading..."); 
     yield return Task.Delay(1000).AsResult(); 
     yield return Loader.Hide(); 
    } 
} 
関連する問題