Alain Professional Alain Professional - 1 month ago 16
C# Question

Frame.navigate and NagivationService do not work

I don't know why Frame.Navigate and NavigationService don't work.

Here my following code,
MainPage.xaml :

<Grid>
<ContentControl Content="{Binding CurrentViewModel}" />

<Button
Command="{Binding ConnexionViewCommand}"
x:Name="Connexion_button"
HorizontalAlignment="Left"
Margin="90,308,0,0"
Content="Login"
VerticalAlignment="Top"
Width="282"/>
<Button
Command="{Binding InscriptionViewCommand}"
x:Name="Create_account"
HorizontalAlignment="Left"
Margin="90,446,0,0"
Content="Create Account"
VerticalAlignment="Top"
Width="282"/>
</Grid>


MainViewModel.cs

public class MainViewModel : ViewModelBase
{
private ViewModelBase _currentViewModel;

readonly static ConnexionViewModel _ConnexionViewModel = new ConnexionViewModel();
readonly static InscriptionViewModel _InscriptionViewModel = new InscriptionViewModel();
readonly static MainPageViewModel _MainPageViewModel = new MainPageViewModel();


public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
RaisePropertyChanged("CurrentViewModel");
}
}
public ICommand ConnexionViewCommand { get; private set; }
public ICommand InscriptionViewCommand { get; private set; }

public MainViewModel()
{
CurrentViewModel = MainViewModel._MainPageViewModel;
ConnexionViewCommand = new RelayCommand(() => ExecuteConnexionViewCommand());
InscriptionViewCommand = new RelayCommand(() => ExecuteInscriptionViewCommand());
}

private void ExecuteConnexionViewCommand()
{
Frame.Navigate(typeof(ConnexionView));
NavigationService.Navigate(new Uri("/ConnexionView.xaml", UriKind.Relative));
MessageBox.Show("CONNEXION");
}

private void ExecuteInscriptionViewCommand()
{
Frame.Navigate(typeof(InscriptView));
NavigationService.Navigate(new Uri("/InscriptionView.xaml", UriKind.Relative));
MessageBox.Show("REGISTER");
}


I have this following error for my Frame.navigate : Cannot Convert System.String to System.Uri.

For my NavigationService.Navigate : An object reference is required for the property. The method or the non-static field "NavigationService.Navigate (Uri)

Thanks for your help.

Answer

You can't mix Frame.nagivate and NavigationService.Navigate together.