Question

I am in the process of converting a VB.NET PRISM Module to C#. It is a very simple affair that currently works in VB.NET. It displays a string value that is bound to a ViewModel.

The XAML for the View (I switched it to hard-coded text in an effort to force its display, normally it is a databound control):

<UserControl x:Class="StatusBarAlarmsView"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                     mc:Ignorable="d" 
                     MinWidth="150">
    <Grid>
        <TextBlock Text="Garrison" VerticalAlignment="Center"/>
    </Grid>
</UserControl>

The following code works in VB to display the View:

regionManager.RegisterViewWithRegion(RegionNames.statusBarRegion, 
    Function() container.Resolve(Of iSBAlarmsPresenter).view)

I attempted the same thing with converted C# code, and it looks like this:

this.regionManager.RegisterViewWithRegion(RegionNames.statusBarRegion,
    () => new StatusBarAlarmsView());

I have tried multiple permutations of the above C#, all without success:

// One
var statusbarRegion = regionManager.Regions[RegionNames.statusBarRegion];
var test = new StatusBarAlarmsView();
test.ViewModel = new AlarmViewModel(this.logger, this.eventAggregator, null, 
    this.container);
statusbarRegion.Add(test);

// Two
this.regionManager.RegisterViewWithRegion(RegionNames.statusBarRegion,
    () => container.Resolve<iSBAlarmsPresenter>().View);

I won't go into the detail of how the View and ViewModel hook up, because currently I'm not concerned about that--I just want it to display "Garrison".

Additional Info Using Snoop, I can drill down into the StatusBar Region. I can see the other items displayed in that region, and they all have their DataContexts set as expected.

With my Alarm Module, however, the ContentPresenter does not have an inherited DataContext, it is marked as Local, which is different from all of the other items in the region. When I attempt to view the DataContext of the bottommost ContentPresenter, I am told the "object is NULL".

All of this is beside the point, though, because my View is being loaded into the tree, and yet my TextBlock is not displaying.

Updates As requested, XAML markup for the StatusBar Region:

<Grid Name="StatusBarGrid" Grid.Row="3" Height="30">
    <e:Interaction.Behaviors>
        <behaviors:GridHeightModifyOnFontChangeBehavior FontSize="{DynamicResource AppFontSize}"/>
    </e:Interaction.Behaviors>
    <DockPanel >
        <WrapPanel  DockPanel.Dock="Right"  cal:RegionManager.RegionName="{x:Static inf:RegionNames.statusBarRightRegion}" Orientation="Horizontal" Background="{DynamicResource ControlBackgroundBrush}"   />
        <StatusBar DockPanel.Dock="Left"  cal:RegionManager.RegionName="{x:Static inf:RegionNames.statusBarRegion}" />
    </DockPanel>
</Grid>
Was it helpful?

Solution

Are you invoking the InitializeComponent method in the constructor of the StatusBarAlarmsView?

A common mistake when addapting views (specially if you need to modify the constructors of those views) is to forget about calling the InitializeComponent method. This method "tells" the view to create and render its inner components. If it's not invoked you will usually find your view looking empty because its components were never created.

OTHER TIPS

I THINK you're trying to show all 5 status views at the same time in your Status Region? That isn't possible. The first View that's registered in the Region will be visible and active. The others you registered won't show up until you navigate to it.

Try doing this somewhere after everything is loaded to see if your StatusBarAlarmsView shows up.

this.regionManager.RequestNavigate(RegionNames.statusBarRegion, new Uri("StatusBarAlarmsView", UriKind.Relative));

Also let the container create your views for you so it can handle the view models and all the dependencies:

this.regionManager.RegisterViewWithRegion(RegionNames.statusBarRegion, typeof(StatusBarAlarmsView));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top