2012-06-18 43 views
3

我在將視圖加載到ContentControl時遇到了一些麻煩。我試圖儘可能保持簡單,所以我使用了CM附帶的Hello項目。我確保Hello項目編譯正確,並運行。它顯示一個帶有文本框的窗口和一個按鈕。文本框和按鈕都在運行時連接到示例ViewModel。Caliburn.Micro將視圖連接到ViewModel以供ContentControl顯示

我修改了ShellView.xaml,並用Grid控件替換了StackPanel控件,並用4行和一列設置了網格。我將文本框分配到第一行,將按鈕分配到第二行,然後將兩個單獨的ContentControl分配到最後兩行。

<Grid Width="800" Height="600"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition></ColumnDefinition> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
    </Grid.RowDefinitions> 
    <TextBox Grid.Row="0" Grid.Column="0" x:Name="Name" /> 
    <Button Grid.Row="1" Grid.Column="0" x:Name="SayHello" Content="Click Me" /> 
<ContentControl Grid.Row="2" Grid.Column="0" x:Name="TopMenu" 
    VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"></ContentControl> 
<ContentControl Grid.Row="3" Grid.Column="0" x:Name="BottomMenu" 
    VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"></ContentControl> 
</Grid> 

我創建在的ViewModels夾兩個單獨的C#類它們是的ViewModels和分別稱爲TopMenuViewModel.cs,和BottomMenuViewModel.cs。這兩個類都擴展了PropertyChangedBase類。這只是模仿示例項目附帶的ShellViewModel.cs類。

using System; 
using Caliburn.Micro; 
namespace TestWithCaliburnMicro.ViewModels 
{ 
/// <summary> 
/// Description of BottomMenuViewModel. 
/// </summary> 
public class BottomMenuViewModel : PropertyChangedBase 
{ 
    public BottomMenuViewModel() 
    { 
    } 

} 

I中是相應的視圖,並且分別稱爲TopMenuView.xaml和BottomMenuView.xaml視圖文件夾中創建兩個單獨的WPF用戶控制。我在「Top Menu」或「Bottom Menu」的內容的每個xaml中添加了一個標籤,以區分這兩者。

<Grid> 
    <Label>Bottom Menu View</Label> 
</Grid> 

在ShellViewModel.cs類我創建了兩個公共屬性,只有「獲取」訪問設置返回相應的視圖模型的實例。

private BottomMenuViewModel _bottomMenu; 
    public BottomMenuViewModel BottomMenu { 
     get { return _bottomMenu; } 
    } 

    private TopMenuViewModel _topMenu; 
    public TopMenuViewModel TopMenu { 
     get { return _topMenu;} 
    } 

將斷點添加到任一屬性的get訪問器中,表明在調試項目時調用get訪問器。我向BottomMenuViewModel.cs類的構造函數添加了一個簡單的語句,例如int x = 0,併爲該行添加了一箇中斷,但中斷不會被中斷,這對我來說意味着構造函數未被調用,所以真的類沒有創建?

我相信我在做什麼是非常基本的,並閱讀所有關於在CM Codeplex上的網站約定的文件,並確認這條評論的邏輯:Prior question on stackoverflow

希望有人將要讀到這篇文章時並指向正確的方向。謝謝。

GitHub上的解決方案。注:SharpDevelop的4.x的 GitHub solution

+0

你可以把解決方案放在github上,我會抓住它,並通過它看。 –

+0

你在哪裏設置'_topMenu'和'_topMenu'?因爲Caliburn不會爲您創建ViewModels。它有兩種模式:首先是ViewModel,然後在創建View時首先創建viewmodel或View的視圖,然後ViewModel和caliburn將它們粘合在一起。 – nemesv

+0

@nemesv:好點。我遵循慣例只提供get訪問器作爲基於示例HelloScreens項目位於此處:[鏈接](http://caliburnmicro.codeplex.com/wikipage?title=Screens%2c%20Conductors%20and%20Composition&referringTitle=Documentation)以及位於此處的HelloScreensWPF:[link](https://skydrive.live.com/?cid=5f154148de0c0da9#),其中顯示了單獨使用get accessor來顯示View。 '只讀IDialogManager對話框; 公共IDialogManager對話{{{0} {0} {0} {0} {get {return dialogs; } }' – voidmain

回答

0

要麼做實例化視圖模型在ShellViewModel的構造函數,或者如果你想在稍後實例化它們,然後加入制定者到您的視圖模型的屬性,並調用NotifyOfPropertyChange方法來通知您的UI那些屬性引用已更改。

相關問題