Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Caliburn Micro-在ViewModels之间共享数据_C#_Wpf_Mvvm_Caliburn.micro - Fatal编程技术网

C# Caliburn Micro-在ViewModels之间共享数据

C# Caliburn Micro-在ViewModels之间共享数据,c#,wpf,mvvm,caliburn.micro,C#,Wpf,Mvvm,Caliburn.micro,从Caliburn Micro的“简单MDI”示例开始。我希望实现以下目标: 我想在ViewModels之间共享类的引用。应将shareme.count作为引用传递给所有ViewModels。这将允许我在每个ViewModel中更改它 我如何才能实现这一接近Caliburn Micro约定的目标 ShellViewModel.cs public class ShellViewModel : Conductor<IScreen>.Collection.OneActive { Shar

从Caliburn Micro的“简单MDI”示例开始。我希望实现以下目标:

我想在ViewModels之间共享类的引用。应将shareme.count作为引用传递给所有ViewModels。这将允许我在每个ViewModel中更改它

我如何才能实现这一接近Caliburn Micro约定的目标

ShellViewModel.cs

public class ShellViewModel : Conductor<IScreen>.Collection.OneActive {

SharedClass _shareme;

public SharedClass shareme {
    get { return _shareme; }
    set {
        _shareme = value;
        NotifyOfPropertyChange(() => shareme);
    }
}

public ShellViewModel() {
    shareme.count = 1;
}

public void OpenTab() {
    ActivateItem(new TabViewModel {
        DisplayName = "Tab " + shareme.count++
    });
}
public class TabViewModel : Screen {}
    public class AppBootstrapper : BootstrapperBase {
    SimpleContainer container;

    public AppBootstrapper() {
        Initialize();
    }

    protected override void Configure() {

        container = new SimpleContainer();

        container.Singleton<IWindowManager, WindowManager>();
        container.Singleton<IEventAggregator, EventAggregator>();
        container.PerRequest<IShell, ShellViewModel>();
    }

    protected override object GetInstance(Type service, string key) {
        var instance = container.GetInstance(service, key);
        if (instance != null)
            return instance;

        throw new InvalidOperationException("Could not locate any instances.");
    }

    protected override IEnumerable<object> GetAllInstances(Type service) {
        return container.GetAllInstances(service);
    }

    protected override void BuildUp(object instance) {
        container.BuildUp(instance);
    }

    protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
    {
        DisplayRootViewFor<IShell>();
    }
}
AppBootstrapper.cs

public class ShellViewModel : Conductor<IScreen>.Collection.OneActive {

SharedClass _shareme;

public SharedClass shareme {
    get { return _shareme; }
    set {
        _shareme = value;
        NotifyOfPropertyChange(() => shareme);
    }
}

public ShellViewModel() {
    shareme.count = 1;
}

public void OpenTab() {
    ActivateItem(new TabViewModel {
        DisplayName = "Tab " + shareme.count++
    });
}
public class TabViewModel : Screen {}
    public class AppBootstrapper : BootstrapperBase {
    SimpleContainer container;

    public AppBootstrapper() {
        Initialize();
    }

    protected override void Configure() {

        container = new SimpleContainer();

        container.Singleton<IWindowManager, WindowManager>();
        container.Singleton<IEventAggregator, EventAggregator>();
        container.PerRequest<IShell, ShellViewModel>();
    }

    protected override object GetInstance(Type service, string key) {
        var instance = container.GetInstance(service, key);
        if (instance != null)
            return instance;

        throw new InvalidOperationException("Could not locate any instances.");
    }

    protected override IEnumerable<object> GetAllInstances(Type service) {
        return container.GetAllInstances(service);
    }

    protected override void BuildUp(object instance) {
        container.BuildUp(instance);
    }

    protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
    {
        DisplayRootViewFor<IShell>();
    }
}
公共类AppBootstrapper:BootstrapperBase{
简单容器;
公共AppBootstrapper(){
初始化();
}
受保护的覆盖无效配置(){
容器=新的SimpleContainer();
container.Singleton();
container.Singleton();
container.PerRequest();
}
受保护的覆盖对象GetInstance(类型服务,字符串键){
var instance=container.GetInstance(服务,键);
if(实例!=null)
返回实例;
抛出新的InvalidOperationException(“找不到任何实例”);
}
受保护的重写IEnumerable GetAllInstances(类型服务){
返回容器。GetAllInstances(服务);
}
受保护的覆盖空洞堆积(对象实例){
容器。堆积(实例);
}
启动时受保护的覆盖无效(对象发送方,System.Windows.StartupEventArgs e)
{
DisplayRootViewFor();
}
}

也将作为您重复问题的答案

public class ViewModelA : Screen, IHandle<ShareMeMessageA>
{
  private readonly IEventAggregator _events;
  private int _sharemecount;
  public class ViewModelA(IEventAggregator events){

       _events = events;
       _events.Subscribe(this);
  }

  //... other bits out for brevity 
  //-- EDIT -- 
  public void SomeEventClick(){
    _event.PublishOnUiThread(new ShareMeMessageB(){ ...  etc ... });
  }

  protected override void Deactivated(bool close){
    _events.Unsubscribe(this);
  }

  private void Handle(ShareMeMessageA msg)
  {
      if(msg != null)
        sharemecount = msg.Count;
  }
}
EventAggregator不仅仅适用于“事件”,您可以将消息传递给正在侦听消息或事件签名的任何或所有viewmodels

public class ViewModelA : Screen, IHandle<ShareMeMessageA>
{
  private readonly IEventAggregator _events;
  private int _sharemecount;
  public class ViewModelA(IEventAggregator events){

       _events = events;
       _events.Subscribe(this);
  }

  //... other bits out for brevity 
  //-- EDIT -- 
  public void SomeEventClick(){
    _event.PublishOnUiThread(new ShareMeMessageB(){ ...  etc ... });
  }

  protected override void Deactivated(bool close){
    _events.Unsubscribe(this);
  }

  private void Handle(ShareMeMessageA msg)
  {
      if(msg != null)
        sharemecount = msg.Count;
  }
}
公共类视图模型A:屏幕,IHandle
{
私人只读IEventAggregator事件;
私人国际共享帐户;
公共类ViewModelA(IEventAggregator事件){
_事件=事件;
_活动。订阅(本);
}
//…为了简洁起见,其他部分将被删除
//--编辑--
public void SomeEventClick(){
_event.publisonuithread(新的ShareMeMessageB(){…等…});
}
受保护覆盖无效停用(布尔关闭){
_事件。取消订阅(此);
}
私有无效句柄(ShareMeMessageA msg)
{
如果(msg!=null)
sharemecount=msg.Count;
}
}

由于这只是一个示例,您根本不需要传递类对象,您可以传递任何想要的类型bool、int、float等。

使用EventAggregator传递消息可能是最好的解决方案。或者在显示新屏幕时将其作为参数传递这也是我发现的。但是,只要您没有双向传输和三个ViewModel之间的关系,这看起来就很好。因为我不能传递引用,只能传递实例,对吗?实例正确,不能传递引用。为澄清而编辑谢谢澄清。一旦传递了一个类的实例,该类包含的属性在发生更改时会触发,如何在发布和接收ViewModel中都获取此NotifyOfPropertyChange事件?有没有简单的方法?
i处理所有需要查看更改的类。你不局限于
IHandle
或消息的数量,但如果你有消息意大利面,可能会达到某种程度。它们可以是不同的类型,不仅仅是我概述的类型,或者你在网上看到的系统类型