passing Data

Eduardo Gomez Romero 1,075 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?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,701 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,181 Reputation points Microsoft Vendor
    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 Answers by the question author, which helps users to know the answer solved the author's problem.