-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question - Pass data between Viewmodels #48
Comments
I think a bit more information may be needed in order to answer your question. There is not enough code posted to determine the control flow of your app up to this point. Typically, if passing a base page or something similar into a ViewModel I would use an interface. But that's just what I can gather from what is posted. I will be glad to help if you can give a bit more context. |
Hi, thanks for your answer. I have a Product List page and I want to which in the list double-clicked product id send product details page but I can't send it product id to product detail view model. How can I send Product Id another view model? I tried field-initialization but not work because if the class has a constructor, first initializing constructor and after sets fields. Example: I tried ProductDetail constructor parameter but I got an error; Sorry for my bad English. I'm using BasePage and BaseViewModel. fasetto-word/Source/Fasetto.Word/Pages/BasePage.cs Lines 1 to 224 in 25d2428
fasetto-word/Source/Fasetto.Word/ViewModel/Base/BaseViewModel.cs Lines 1 to 111 in 25d2428
|
I have personally used some sort of state storage for issues like this. I use the MVVM pattern for all of my WPF applications and this is always an issue when loading a new page that is dependent on the data obtained from another. So your product list page's viewmodel would store the ID in the state manager, and when loaded, the product detail page would retrieve the ID from the state manager. That decouples the two pages from one another. You could store this state in a database, in a static StateManager class, or some other solution that you feel is best for your application. I'm not sure what methods other MVVM developers use but this has always been the best for my use case. Other methods that I've seen are overly complicated, add way too many steps, and are difficult to maintain and debug. Without seeing the entire code base I cannot make a solid recommendation, but hopefully this will get you started. |
Honestly, I tried to use a similar pattern to create a WPF application in the past and had some issues like this. I never load pages by passing view models. I always allow a page to load a new view model in their constructor and retrieve the data from some sort of state storage that will work for your application. Again, it is hard to determine without seeing the entire code base, but you might try something similar to avoid these issues. |
Hi, thanks for your answer and the interest you have shown. I think I found a solution. I changed my Base Page to BasePage in Xaml File and constructor like this; #region Constructor
/// <summary>
/// Default constructor
/// </summary>
public ProductDetail(Product product)
{
InitializeComponent();
ViewModelObject = new ProductDetailViewModel(product);
}
#endregion In This way I can define fields viewmodel constructor. #region Constructor
/// <summary>
/// Default constructor
/// </summary>
public ProductDetailViewModel(Product product)
{
// Set product
Product = product;
}
#endregion |
Hi,
I have a view model field and I want to send another view model. I cant use object initialize because the constructor executions first and this field is null. I need this field in the constructor. I tryed constructor parameter but that didnt work i get a error;
Error:
'ProductDetailViewModel' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TVm' in the generic type or method 'BasePage<TVm>'
P.S: sorry for my English :|
Thanks
The text was updated successfully, but these errors were encountered: