-1

I have two Pages (MainWindow & Client) and I'm trying to use the NavigationService to navigate between my pages.

When I navigate from MainWindow to the Client it works, but when I try to navigate from the Client to the MainWindow it gives me an exception :

Cannot cast object of type WpfApplication1.MainWindow to WpfApplication1.Client

This is my MainWindow

<Grid>
    <Button HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Click="Navigate_Click">Navigate</Button>
</Grid>

private void Navigate_Click(object sender, RoutedEventArgs e)
{
    if (NavigationService == null)
    {
        return;
    }

    NavigationService.Navigate(new Uri("Client.xaml", UriKind.Relative), "Hi from calling window!");
    NavigationService.LoadCompleted += NavigationService_LoadCompleted;
}

private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
    ((Client)e.Content).MessageFromCallingWindow = (string)e.ExtraData;
}

And the client

<Grid>
    <StackPanel>
        <TextBlock VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   Text="{Binding MessageFromCallingWindow}" />

        <Button Click="OnClick">Go back</Button>
    </StackPanel>
</Grid>

public static DependencyProperty MessageFromCallingWindowProperty = DependencyProperty.Register("MessageFromCallingWindow", typeof(string), typeof(Client));

public string MessageFromCallingWindow
{
    get { return (string)GetValue(MessageFromCallingWindowProperty); }
    set { SetValue(MessageFromCallingWindowProperty, value); }
}

private void OnClick(object sender, RoutedEventArgs e)
{
    if (NavigationService != null)
    {
        NavigationService.Navigate(new Uri("MainWindow.xaml", UriKind.Relative), "returning hello from client");
    }
}

I tried also NaviugationService.GoBack(), but it gives the same exception !

Any clue ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174

1 Answers1

1

Every time your navigation completes you call this code:

((Client)e.Content).MessageFromCallingWindow = (string)e.ExtraData;

Quite simply, that code will fail when you navigate to the MainWindow because you can't cast it to Client.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • And what should I do to go back and send the MainWindow a message like a did for the client ? – Wassim AZIRAR Dec 02 '13 at 14:07
  • There's a question [here](http://stackoverflow.com/questions/12444816/how-to-pass-values-parameters-between-xaml-pages/12444817#12444817) that discusses some of the better methods for communicating between pages in WPF. – Dan Puzey Dec 02 '13 at 14:11
  • This is not what I asked sir ! – Wassim AZIRAR Dec 02 '13 at 14:14
  • 1
    No, but it's the better answer to your problem. The alternatives are: implementing your `MessageFromCallingWindow` property in a base class and having all your pages derive from that (means you cast `e.Content` to `BaseClass` instead of to `Client`), or you detect the type of the page and cast appropriately (e.g.: `if (e.Content is Client) { // do your existing cast } else if (e.Content is MainWindow) { // do a different cast }` etc... This would not be a recommended approach! – Dan Puzey Dec 02 '13 at 14:21