2012-04-03 17 views
12

पर मेनव्यू में उपयोगकर्ता नियंत्रण को बाध्य करने के लिए प्राप्त कर रहा है। मेरे पास मेनव्यू.एक्सएमएल है, जो कि मेनव्यू मॉडेल को बाध्यकारी है।कैलिबर्न। मिक्रो इसे अपने व्यूमोडल्स

जो मैं कोशिश करना चाहता था वह मेरे मुख्य रूप पर UserControls में बहुत सारे नियंत्रण को विभाजित कर रहा था।

अब मैं UserControls को मुख्य दृश्य के साथ दृश्य फ़ोल्डर के अंदर रखता हूं और उन्हें नाम दिया गया है, LeftSideControlView.xaml और RightSideControlView.xaml। इसी ViewModels, LeftSideControlViewModel बुलाया ViewModels फ़ोल्डर में हैं आदि

मैं सफलतापूर्वक mainview को usercontrols कहा:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <UserControls:LeftSideControlView cal:Bind.Model="{Binding}" /> 
    <UserControls:RightSideControlView cal:Bind.Model="{Binding}" 
            Grid.Column="1" /> 
</Grid> 

वे डिजाइनर में सही तरीके से प्रदर्शन। यहाँ XAML में उनमें से एक है:

<UserControl x:Class="TwitterCaliburnWPF.Library.Views.LeftSideControlView" 
     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" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid> 
    <StackPanel> 
     <Label x:Name="Text" FontSize="25" HorizontalAlignment="Center" Margin="5"/> 
     <TextBox x:Name="TextBox1" 
       Width="200" 
       HorizontalAlignment="Center" FontSize="25" Margin="5" /> 
     <Button Width="200" Height="50" Content="Button!" Margin="20" /> 
    </StackPanel> 
</Grid> 

मैं Caliburn Castle.Windsor प्रयोग करने के लिए AppBootstrapper अंदर ViewModels और उनके अंतरफलक को जोड़ा।

using Screen = Caliburn.Micro.Screen; 

namespace TwitterCaliburnWPF.Library.ViewModels 
{ 
    public class LeftSideControlViewModel : Screen, ILeftSideControlViewModel 
    { 
     private string _text = "Hello from the Left side!"; 
     private string _textBox1 = "Enter Text Here"; 

     public string Text 
     { 
      get { return _text; } 
     } 

     public string TextBox1 
     { 
      get { return _textBox1; } 
     } 
    } 
} 

यहाँ MainViewModel है और मैं बंद जा रहा हूँ कि मैं क्या Caliburn.Micro documention में पढ़ा है, पहले की तरह मैंने कोशिश की यह कुछ भी नहीं करने के लिए MainViewModel में विशेष रूप से किया गया था:

public class ApplicationContainer : WindsorContainer 
{ 
    public ApplicationContainer() 
    { 
    // Register all dependencies here 
    Register(
     Component.For<IWindowManager>().ImplementedBy<WindowManager>().LifeStyle.Is(LifestyleType.Singleton), 
     Component.For<IEventAggregator>().ImplementedBy<EventAggregator>().LifeStyle.Is(LifestyleType.Singleton), 
     Component.For<ILeftSideControlViewModel>().ImplementedBy<LeftSideControlViewModel>(), 
     Component.For<IRightSideControlViewModel>().ImplementedBy<RightSideControlViewModel>() 
     ); 

    RegisterViewModels(); 
    } 

    private void RegisterViewModels() 
    { 
    Register(AllTypes.FromAssembly(GetType().Assembly) 
        .Where(x => x.Name.EndsWith("ViewModel")) 
        .Configure(x => x.LifeStyle.Is(LifestyleType.Transient))); 
    } 

यहाँ LeftSideControlViewModel वर्ग है इन 2 नियंत्रणों को लोड करने या इन 2 नियंत्रणों को दिखाने के लिए कहें।

फिर भी जब ऐप चालू हो जाता है और मान चलते हैं तो उपयोगकर्ता नियंत्रणों को उनके संबंधित व्यूमोडल्स से बाध्यकारी नहीं होते हैं।

namespace TwitterCaliburnWPF.Library.ViewModels 
{ 
    public class MainViewModel : Conductor<IScreen> 
    { 
     public MainViewModel() 
     { 
     ShowLeftControl(); 
     ShowRightControl(); 
     } 

     private void ShowRightControl() 
     { 
     ActivateItem(new RightSideControlViewModel()); 
     } 

     private void ShowLeftControl() 
     { 
     ActivateItem(new LeftSideControlViewModel()); 
     } 

     public string TextToDisplay 
     { 
     get { return "Coming from the ViewModel!"; } 
     } 
    } 
} 

उत्तर

22

आपको यहां Conductor का उपयोग करने की आवश्यकता नहीं है। यह मूल रूप से नेविगेशन परिदृश्यों के लिए उपयोग किया जाता है। बस MainViewModel पर RightSideControlViewModel पर राइटसाइड नामक दो सार्वजनिक गुण बनाएं और एक LeftSideControlViewModel, जिसे वामसाइड कहा जाता है। फिर, अपने उपयोगकर्ता नियंत्रण को सीधे अपने मेनव्यू में तुरंत चालू करने के बजाय, दो ContentControls बनाएं, एक x:Name="LeftSide" और दूसरा x:name="RightSide" के साथ यह एक दृश्य-मॉडल-इसे पूरा करने का पहला तरीका है। यदि आप इसे पहले देखना चाहते हैं, तो अपने मेनव्यू में उपयोगकर्ता नियंत्रण परिभाषाएं रखें, लेकिन बाइंड को बदलें। मॉडल को Bind.Model="{Binding LeftSide}"

मूल रूप से, जिस तरह से आपके पास चीजें परिभाषित होती हैं .... बाइंडिंग सिर्फ सही वस्तुओं पर इंगित नहीं कर रहे हैं, कम या ज्यादा। आपको वहां कंडक्टर मिला है, जिसे आपको पूरा करने की आवश्यकता नहीं है। यदि आप किसी प्रकार का नेविगेशन आर्किटेक्चर करना चाहते हैं तो आप इसे रखना चाहेंगे। याद रखें कि जब आप एक कंडक्टर पर ActivateItem को कॉल करते हैं, तो आप मूल रूप से अपनी ActiveItem प्रॉपर्टी बदल रहे हैं; एक समय में सक्रिय होने वाला केवल एक मॉडल। उपर्युक्त मामले में आप दोनों वस्तुओं को सक्रिय करते हैं, लेकिन केवल दूसरा सक्रिय रहता है। इसके अलावा, आपके विचार में, ActiveItem से कुछ भी बाध्य नहीं है।

मुझे उम्मीद है कि यह समझ में आता है!

संबंधित मुद्दे