Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so i have an application that has a menu that has a menu and i have couple of variables like

public string filepath {get;set;}
public bool isinstalled {get;set;}

and in the download viewmodel i give these values and determining on what is selected file path can change and bool installed is true or false depending on what the registry returns,

however i want to share the value of this to say the installvm

and have it enable and disable a button determining on what the bool result is

i been trying for days and followed a few youtube videos on pub/sub

but i just cant seem to get this to work its always null or does not pass the values

i could just put static and call each viewmodel but that would make dependencies

just really confused on how to do this

if anyone has had this issue or knows of a good way of resolving it would be amazing and any help would be much appreciated

What I have tried:

I tryied looking into mvvm framework
prism mainly but not easy to start with,

have tried following this tutorial on youtube,



but kept getting instants not set to reference of an object

i already have my own application so is difficult to implement what was in the video only reason i am showing this is because i have removed it now so don't have code implemented anymore
Posted
Updated 13-Mar-23 17:51pm
Comments
[no name] 13-Mar-23 23:28pm    
The view models query the data model (where your settings are). Or you inject them when creating the view model by default. IMO.

1 solution

It depends on the MVVM library that you are using or your own implementation.

1. Most MVVM libraries have a message broker to pass strongly-typed data between different parts of your applications, like between ViewModels. Here is the MVVM Community Toolkit documentation on their implementation: Messenger - .NET Community Toolkit | Microsoft Learn[^]

2. Alternatively, if you are using Dependency Injection[^], you can use a Singleton dependency class to hold the shared data and inject that into your ViewModels that need to share the data.
C#
public class SharedDataStore : ISharedDataStore
{
    // put public properties and methods here...
    public some_value_type SomeProperty { get; set; }
}

public interface ISharedDataStore
{
    // list public properties and methods here...
    some_value_type SomeProperty { get; set; }
}

Now you can set up the dependencies:
C#
HostApplicationBuilder builder = Host.CreateApplicationBuilder();

builder.Services.AddSingleton<ISharedDataStore, SharedDataStore>();

IHost host = builder.Build();

Then inject in to the ViewModel:
C#
public class MyViewModel : ViewModelBase
{
    private ISharedDataStore _sharedData;

    public MyViewModel(ISharedDataStore sharedData)
    {
        this._sharedData = sharedData;
    }

    public void DoSomething()
    {
        this._sharedData.SomeProperty = some_value;
    }
}

3. If not using dependency Injection, then create a static (shared) class manually to hold the shared data. Then each ViewModel can access the static class.
C#
public static class SharedDataStore
{
    // put public properties and methods here...
    public some_value_type SomeProperty { get; set; }
}

Then in your ViewModel, you can access the shared data:
C#
public class MyViewModel : ViewModelBase
{
    public void DoSomething()
    {
        SharedDataStore.SomeProperty = some_value;
    }
}

Hope this helps!
 
Share this answer
 
v3
Comments
elfenliedtopfan5 14-Mar-23 11:05am    
thank you for your reply that has helped me out so much it was really confusing what you have put here is really easy to understand and put it in a way i can understand each part thank you for taking your time to write this reply
Graeme_Grant 14-Mar-23 11:06am    
You are most welcome. 😊
Azeem Anwer Choudhary 2-Jul-23 15:58pm    
Thanks, Graeme. I bumped into this post searching for data-sharing options between ViewModels.

Please excuse me for my question as I am new to the MVVM pattern. Where would actually the code "Now you can set up the dependencies:" go?
Graeme_Grant 2-Jul-23 20:36pm    
The example above is for .Net3.1+, not .Net Framework. For .Net Framework, it depends on the IOC container system that you are using. There are a few out there, NuGet Gallery | Autofac[^] is very popular and their documentation[^] is very good.

If .Net 3.1+ (Core), currently .Net 7.0, then it's usually done in the Program.cs class or you create your own extension class to package up the wiring up. Read more here: Dependency injection in ASP.NET Core | Microsoft Learn[^]

I highly recommend moving to .Net 7.0+ from .Net Framework. Just by recompiling your ,Net Framework code in .Net, your app will run faster!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900