2

我有一个 mvvm(model view viewmodel) silverlight 应用程序,它有几个需要加载到 ContentControls 的视图(我在表达式混合中完成了这一切)。我不知道该怎么做,例如,通过单击另一个内容控件中的另一个视图中的按钮,在一个内容控件中加载一个视图(用户控件)。为了更容易理解问题,我需要做类似的事情:

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

不同之处在于 child1 和 child2 应该通过单击 Call child1 或 call child2 按钮加载到他们自己的内容控件中。

和例子将不胜感激。提前致谢!

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 库

第二个视图模型接收消息并为其属性创建一个视图:

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 回答