1
votes

I am defining two regions in shell: MainRegion and ToggleRegion. The Toggle region contains a button on clicking on that button i want to change the region in Main Region.

Here is my xaml code for registering regions in shell.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" ></RowDefinition>
        <RowDefinition Height="30"></RowDefinition>
    </Grid.RowDefinitions>

    <ContentControl Grid.Row="0" Regions:RegionManager.RegionName="MainRegion"></ContentControl>
    <ContentControl Grid.Row="1" Regions:RegionManager.RegionName="ToggleRegion"></ContentControl>

</Grid>

My Bootstrapper add MainModule where I am injecting view in Region

  protected override IModuleCatalog CreateModuleCatalog()
    {
        var catalog = new ModuleCatalog();
        catalog.AddModule(typeof (MainModule));
                   return catalog;
    }

My MainModule class

 public void Initialize()
    {
        regionManager.RegisterViewWithRegion("MainRegion", typeof(MainView));
        regionManager.RegisterViewWithRegion("ToggleRegion", typeof(ToggleView));

     }

On Running the application I can see MainView and ToggleView loaded in MainRegion and ToggleRegion. But when i click the button in toggle region to change the view in Main region. Main region view is not changing.

Code in my button click event

{
     IRegion region = regionManager.Regions["MainRegion"];

        var view = region.Views.SingleOrDefault();
        region.Remove(view);
        regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewOnButtonClick));
        region.Activate(view);

}

On debugging I can see region is removing MainView first then activating viewonbuttonclick but same is not reflecting in my xaml View.

What am I missing ?

2

2 Answers

0
votes

I think the problem is here

var view = region.Views.SingleOrDefault();
    region.Remove(view);
    regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewOnButtonClick));
    region.Activate(view);

What I would do is register all the views upfront..

regionManager.RegisterViewWithRegion("MainRegion", typeof(MainView));
regionManager.RegisterViewWithRegion("ToggleRegion", typeof(ToggleView));
regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewOnButtonClick));

and then instead of removing a view from the region get the view by the view name you've given it.. and then activate

      var view = region.Views.SingleOrDefault(v => v != null && v.GetType() == typeof     (ViewOnButtonClick); 

      region.Activate(view);
0
votes

Problem was with my Bootstrapper Class. I was starting the application like this:-

protected override DependencyObject CreateShell()
    {
        Shell shell = new Shell();
       Application.Current.MainWindow = null;
       Application.Current.StartupUri = new Uri("Shell.xaml", UriKind.RelativeOrAbsolute);
       return (DependencyObject)shell;
    }

Using shell.show() instead of current.startupuri allow me to change the view on button click.