passing Data

Eduardo Gomez Romero 1,375 Reputation points
2024-11-19T21:58:01.0833333+00:00

When you pass data from viewModel to view Model, you would do something like

public partial class HomePageViewModel : ObservableObject {

    IAppService Service;
    IDataService _dataService;

    public ObservableCollection<Note> Notes { get; set; } = [];

    public HomePageViewModel(IAppService appService, IDataService dataService) {

        _dataService = dataService;
        Service = appService;
        GetAllNotes();
    }

    [RelayCommand]
    void GotoDtail(object note) {
        if (note is Note n) {
            Service.NavigateToAsync(nameof(DetailPage),
                new Dictionary<string, object> {
                { "note", n }
            });


and in the next viewModel


    [QueryProperty(nameof(ObjectPassed), "note")]
    public partial class DetailPageViewModel : ObservableObject {

        [ObservableProperty]
        Note? objectPassed;
    }
}

How would this work between windows?

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-11-20T06:14:28.52+00:00

    Hello,

    If you open the second window programmatically, you can pass data directly via the constructor.

     var secondWindow = new NewWindow1("this a data from first window");
    
     Application.Current.OpenWindow(secondWindow);
    

    In the Second Window.

    public partial class NewWindow1 : Window
    
    {
    
    	public NewWindow1(string dataService )
    
    	{
    
    		InitializeComponent();
    
          
    
            Page = new NewPage1(dataService);
    
    	}
    
    }
    

    Or you use CommunityToolkit's WeakReferenceMessenger

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.