Xamarin update viewmodel with command - xamarin

currently I don't know how to solve my problem. I am writing an app with xamarin.forms.
I have a view which contains the userprofile of the user. For example there is a entry with the username. The field is binded in the twoway mode to the viewmodel:
<Entry x:Name="givennameSurname" Text="{Binding FullName, Mode=TwoWay}" />
The whole userprofile contains some fields which are validated with a behavior:
<behaviors:TelNumBehavior x:Name="NumValidatorUser" IsValid="{Binding Source={x:Reference root}, Path=BindingContext.UserTelNumBehavior, Mode=TwoWay }"></behaviors:TelNumBehavior>
The isValid property is also binded to the viewmodel.
What I want to achieve? I want, that if one of the behaviors validate the input to false, that the userprofile can not be updated.
So I wanted to create a command on a button. The button has an canExecute method: this method checks the booleans in the viewmodel if they all are true or false. If true, I want to force a refresh of the data of viewmodel from the view. I use that canExecute also to prevent unwanted changes, if the UI is in the state, that some input is wrong:
public string FullName
{
get => profile.GivenName;
set
{
if (CanSave())
{
profile.GivenName = value;
OnPropertyChanged();
}
}
}
What is the problem?
I change an input with a behavior, so that the behavior says the input is wrong. Then I edit the username. Then I change the input of the wrong-behavior to true. Now the input of the username does not refresh in the viewmodel. I want to refresh it with the command, but I don't know how to force a refresh from the view to the viewmodel.

As far as I know you need to write what property you changing in your getter and setter so:
OnPropertyChanged("FullName");

Related

In a Xamarin MVVM application, how can I change what I see on the screen from the ViewModel?

My application viewModel responds to a user clicking a button to see test results:
private void AddDetailRows(List<QuizHistory> quizHistoryList)
{
quizDetails.Children.Clear();
quizDetails.Children.Add(AddData(quizHistoryList));
quizDetails.Children.Add(new LineTemplate());
}
Where quizDetails is the name of an element in the view.
But this doesn't work for me as the view model doesn't know what the view looks like and does not have access to the names of elements.
In a MVVM application, how is this problem solved?
You are completely right, that is not something that ViewModel is responsible of.
So, whatever you want to do with UI is not responsibility of the ViewModel.
If this is really the only option, then you can think of creating boolean properties in your VM and binding them to your views and then changing that boolean from false to true or vice versa on button click command which is binded to your VM.
To simplify it:
MyView.xaml
<StackLayout>
<Button Command="{Binding ShowHideQuizHistoryCommand}" ... />
<StackLayout x:Name="QuizHistory"
IsVisible={Binding ShowQuizHistory }>
//
</StackLayout>
</StackLayout>
MyViewModel.cs
private bool _showQuizHistory ;
public bool ShowQuizHistory
{
get { return _showQuizHistory ; }
set
{
_showQuizHistory = value;
OnPropertyChanged();
}
}
public ICommand ShowHideQuizHistoryCommand => new Command(() =>
{
ShowQuizHistory = !ShowQuizHistory;
});
So, this is just an example based on what you provided in question.
You can also use visual states, converters, triggers and behaviors in order to achieve this, but in my opinion this is the easiest way.

Xamarin Forms: Multiple items for a converter

I have a custom view which shows a "No Results" message when a page has no values.
<common:NoResults
IsVisible="{Binding Details,
Converter={ StaticResource EmptyListIsTrueConverter }}"></common:NoResults>
The problem is when the page is loading, there are no values so it shows and should not.
Is there a way to combine a check for an empty list and another Model property like IsBusy into one Converter ?
In IsVisible set the binding to a property DoneLoadingAndNoValues in the viewmodel with default value false.
Set this property to true when loading is done and Details contains no values.
ViewModel (implements INotifyPropertyChanged with OnPropertyChanged):
private bool doneLoadingAndNoValues = false; // default is false
public bool DoneLoadingAndNoValues
{
get { return doneLoadingAndNoValues; }
set
{
doneLoadingAndNoValues= value;
OnPropertyChanged(nameof(DoneLoadingAndNoValues));
}
}
...
// Done loading and Details contains no values:
DoneLoadingAndNoValues = true;
XAML:
<common:NoResults IsVisible="{Binding DoneLoadingAndNoValues}" />
I can't see a way to easily do this in XAML. Try it in your code behind.
When the page is finished loading, execute a function which checks the how many values there are. If there are no values, show your no values message.

ToolbarItem isEnabled property is available in xaml, not code

I have a form where a user can enter some data and save it to a database. I have been trying to add basic validation, making a user have to enter a value in every field. I want my save button to be unavailable until the user has entered some information in every form.
Here is my button:
<ToolbarItem Name="MenuItem1" Order="Primary" Text="Save" Priority="1" Command="{Binding SaveDataCommand}" IsEnabled="{Binding CanSaveData}"/>
I can access the isEnabled property in the XAML but where this gets infuriating is that I can't then reupdate this property. The button gets stuck in whatever state I tell it on load. I have checked my view model and it is returning a boolean which is correct (printing it to the console), it's just there is no way of updating the button state.
I even tried adding a new button that would force update the enabled state however this wouldn't work
<ToolbarItem x:Name="whyxamarinwhy" Name="MenuItem1" Order="Primary" Text="Save" Priority="1" Command="{Binding SaveEvent}" IsEnabled="{Binding CanCreateEvent}"/>
whyxamarinwhy.isenabled = true; //I cannot access this property because it doesn't exist.
Is the only way to implement this functionality going to be using a custom renderer?
MenuItem.IsEnabledProperty / For internal use by the Xamarin.Forms platform.
You should use the Command's CanExecute of the ToolbarItem to determine if the Execute method can be triggered.
Since you are already binding the command to SaveDataCommand, you can toggle the return of the CanExecute of that command to determine if the menu item button should be allowed to trigger the attached command.
You did not post your ViewModel, but in a inner-class ICommand implementation, something as simple as this works:
static bool SaveDataCommandCanExecute = true;
class SaveDataCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
// your code needs to toggle SaveDataCommandCanExecute to determine if the Execute method can be triggered
return SaveDataCommandCanExecute;
}
public void Execute(object parameter)
{
// Do something
}
}

Windows Phone list picker with empty default value

I have scenario where I need to select country from drop down or list picker. I have found how to make this list, but I need to have a default value in this drop-down and I want the default value to be null.
Can someone suggest the best way how to make something like this.
You should bind the SelectedItem value of the drop-down control to a Property in your ViewModel, then avoid setting the property to other than default value in constructor.
This way it will not show any other item, because your SelectedItem property is null.
EDIT:
If I understand you correctly you could do something like this (I'm using Windows Phone Toolkit at http://phone.codeplex.com/
<toolkit:ListPicker ItemsSource="{Binding MyList}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" />
Then your ViewModel could lokk like this:
public ObservableCollection<string> MyList{get;set;}
public string MySelectedItem{
get{ return _mySelectedItem; }
set{ _mySelectedItem = value;
OnPropertyChanged("MySelectedItem");
}

Refresh bindings on back navigation using MVVM-Light

Scenario: I start out in my app's main page. I navigate to sub-page A, change a value, hit the back button and the bound TextBlock in the main page doesn't change. If I navigate to sub-page B, a TextBlock using that same binding changes. Likewise, if I go to page A again I see the changed value. If I exit the app, the new value shows up on the main page. It's just when using the back button that a refresh doesn't get triggered.
I've got all my INotifyPropertyChanged stuff working. Like I said, the binding works in every scenario besides navigating back to the main page. How do I send a message or otherwise trigger a refresh of the bindings on that page? Thanks!
Edit:
Based on the accepted answer from willmel, here's what I did:
My MainPage.xaml file has this markup:
<TextBlock Text="{Binding Title, Mode=OneWay}" />
My MainViewModel.cs file has this:
public string Title
{
get { return ProfileModel.Instance.DescriptionProfile.Title; }
}
And I added this to the MainViewModel constructor:
Messenger.Default.Register<PropertyChangedMessage<string>>(this,
(action) => DispatcherHelper.CheckBeginInvokeOnUI(
() => RaisePropertyChanged("Title")));
In another view I have the following markup:
<TextBox Grid.Row="1" Width="250" Height="100" Text="{Binding TitleEdit, Mode=TwoWay}" />
In its view model I use this when getting/setting a string:
public string TitleEdit
{
get { return ProfileModel.Instance.DescriptionProfile.Title; }
set
{
if (ProfileModel.Instance.DescriptionProfile.Title == value) return;
string oldValue = ProfileModel.Instance.DescriptionProfile.Title;
ProfileModel.Instance.DescriptionProfile.Title = value;
RaisePropertyChanged("Title", oldValue, value, true);
}
}
In your view model you want to be modified if a child page changes a property. (note here, the property is of type bool, but could be anything)
Messenger.Default.Register<PropertyChangedMessage<bool>>(this,
(action) => DispatcherHelper.CheckBeginInvokeOnUI(
() =>
{
MessageBox.Show(action.newValue.ToString());
//do what you want here (i.e. RaisePropertyChanged on a value they share)
}));
When you use RaisePropertyChanged in the child class, use the broadcasting overload.
RaisePropertyChanged("Preference", oldValue, value, true);
Finally, note that to use DispatcherHelper, you need to Add the following to your App constructor (App.xaml.cs)
DispatcherHelper.Initialize();

Resources