2

ContentControls にロードする必要があるいくつかのビューを持つ mvvm(モデル ビュー ビューモデル) Silverlight アプリケーションがあります (すべて式ブレンドで作成しました)。方法がわからないのは、たとえば、別のコンテンツ コントロールにある別のビューからボタンをクリックして、1 つのビュー (ユーザー コントロール) を 1 つのコンテンツ コントロールに読み込むことです。問題を理解しやすくするために、次のようなことをする必要があります。

http://www.codeproject.com/KB/silverlight/BlendableVMCom.aspx

ただし、子 1 と子 2 は、[子 1 を呼び出す] ボタンまたは [子 2 ​​を呼び出す] ボタンをクリックすることによって、独自のコンテンツ コントロールに読み込まれるはずです。

そして例をいただければ幸いです。前もって感謝します!

4

2 に答える 2

3

この例は非常に単純化されていますが、アプリケーションに合わせて調整する方法があると思います。

メイン ビュー:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Border x:Name="commandsView">
        <Button Content="Call view 1" Command="{Binding CallView1Command}" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5" />
    </Border>
    <Border x:Name="displayedView" Grid.Column="1">
        <ContentControl Content="{Binding CurrentView}" />
    </Border>
</Grid>

ユーザー コントロールとして分割されたビューを作成していません。ここでは単なる境界線であり、実際のビューに置き換えることができます。

コード ビハインドのさまざまなビューのさまざまなビュー モデル:

this.commandsView.DataContext = new CommandsViewModel();
this.displayedView.DataContext = new DisplayedViewModel();

最初のビュー モデルには、メッセージを別のビュー モデルに送信するコマンドが含まれています。

public class CommandsViewModel
{
    public CommandsViewModel()
    {
        this.CallView1Command = new RelayCommand(() => 
          Messenger.Default.Send<View1Message>(new View1Message()));
    }

    public RelayCommand CallView1Command { get; set; }

}

public class View1Message : MessageBase
{

}

この例を機能させるには、MVVM Light ライブラリをダウンロードします。

2 番目のビュー モデルはメッセージを受け取り、そのプロパティのビューを作成します。

public class DisplayedViewModel : ViewModelBase
{
    public DisplayedViewModel()
    {
        Messenger.Default.Register<View1Message>(this, obj => 
            this.CurrentView = new TextBlock { Text = "Pressed the button 1 and now here is the view 1" });
    }

    private object currentView;

    public object CurrentView
    {
        get { return currentView; }
        set
        {
            currentView = value;
            RaisePropertyChanged("CurrentView");
        }
    }
}

ここでも、コントロールの代わりに clr オブジェクトを使用し、xaml でデータ テンプレートを適用することは可能ですが、結果のすべてのコードを提供する十分なスペースがありません。

Messengerこれですべてです。主なアイデアは、この特定のケースのクラスである、ある種のイベント アグリゲーターです。

MVVM Light がなければ、より多くのコードが必要になります。

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        var events = new GlobalEvents();
        this.commandsView.DataContext = new CommandsViewModel(events);
        this.displayedView.DataContext = new DisplayedViewModel(events);
    }
}

public class GlobalEvents
{
    public event EventHandler View1Event = delegate { };

    public void RaiseView1Event()
    {
        View1Event(this, EventArgs.Empty);
    }
}

/// <summary>
/// Commands which call different views
/// </summary>
public class CommandsViewModel
{
    public CommandsViewModel(GlobalEvents globalEvents)
    {
        this.CallView1Command = new DelegateCommand(globalEvents.RaiseView1Event);
    }

    public DelegateCommand CallView1Command { get; set; }
}

/// <summary>
/// Model where views are changed and then displayed
/// </summary>
public class DisplayedViewModel : INotifyPropertyChanged
{
    public DisplayedViewModel(GlobalEvents globalEvents)
    {
        globalEvents.View1Event += (s,e) =>
            this.CurrentView = new TextBlock { Text = "Pressed the button 1 and now here is the view 1" };
    }

    private object currentView;

    public object CurrentView
    {
        get { return currentView; }
        set
        {
            currentView = value;
            RaisePropertyChanged("CurrentView");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

DelegateCommandこの例では、クラスを別のものに変更する必要があります。他のコードは誰にとっても機能します。

于 2011-05-10T22:07:05.600 に答える
0

ある種のナビゲーションをしようとしているように思えます。その場合は、Silverlight ナビゲーション フレームワークを調べてください。

于 2011-05-12T05:26:20.810 に答える