2012-04-04 13 views
6

में व्यूमोडल्स के बीच आवेदन स्थिति उत्तीर्ण करना मुझे कॉन्फ़िगरेशन फ़ाइल पढ़ने और इसके साथ कुछ रिपोर्ट उत्पन्न करने के लिए एक छोटा सा एप्लीकेशन लिखना होगा। मैं अंत में एमवीवीएम का उपयोग करने की उम्मीद कर रहा था लेकिन शुरू करना काफी मुश्किल है। ओह, मैं कैलिबर्न का उपयोग कर रहा हूँ। माइक्रो फ्रेमवर्क।एमवीवीएम डब्ल्यूपीएफ एप्लीकेशन

1) ओपन फाइल 2) सेटिंग दिखाएं 3) दिखाएँ परिणाम

:

तो यह है कि मैं क्या है, इस पर 3 बटन के साथ एक रिबन है कि एक खोल (प्राथमिक राय यह है कि अन्य दृश्यों को होस्ट करता है) है

और रिपोर्ट बनाने और हटाने के लिए बटन के साथ दो अन्य दृश्य, सेटिंग्स दृश्य और परिणाम देखें।

तो मुझे लगता है कि दृश्य संरचना इस तरह होगा:

1. "Show settings" button is disabled until a file is opened (via Open file). 
2. "Show results" button is disabled until a report is calculated (via a 
    method in SettingsViewModel). 
3. If a report is calculated, the CalculateResultsButton is disabled and 
    CancelResultsButton is enabled and vice versa. 

कृपया सलाह मैं यह कैसे प्राप्त कर सकते थे:

ShellView 
    Ribbon 
     OpenFileButton 
     SettingsButton 
     ResultsButton 
    ContentControl (hosts SettingsView and ResultsView) 

SettingsView 
    CalculateResultsButton 

ResultsView 
    CancelResultsButton 

मुश्किल हिस्सा है? मुझे कोई विचार नहीं है कि मुझे किस रणनीति के लिए जाना चाहिए। मेरा गैर-एमवीवीएम-सोच-मस्तिष्क कहता है कि मुझे एक स्टेटस वैरिएबल बनाना चाहिए और फिर किसी भी तरह से उन चर को उस बटन पर बांधना चाहिए, लेकिन मुझे लगता है कि एमवीवीएम दुनिया में काम नहीं करेगा, है ना? किसी भी कोड उदाहरण की बहुत सराहना की जाएगी!

बहुत धन्यवाद!

उत्तर

1

आप मुख्यमंत्री का उपयोग कर रहे के बाद से आप किसी भी कोड-पीछे की जरूरत नहीं होगी। यदि आप चाहें तो .xaml.cs फ़ाइलों को हटा सकते हैं।

यह एक सुंदर मूल उदाहरण है लेकिन यह आपको बटन की स्थिति को नियंत्रित करने के बारे में एक विचार देना चाहिए। इस उदाहरण में, Open सक्षम हो जाएगा और अन्य दो अक्षम हैं। यदि आप Open पर क्लिक करते हैं, तो Settings सक्षम है। Results के साथ Settings पर क्लिक किया जाता है।

आप वैश्विक राज्य इसी अवधारणा एक सिंगलटन, SharedViewModel इंजेक्शन लगाने SharedViewModel में मूल्यों की जाँच कर सकते हैं ViewModels और CanXXX तरीकों में, द्वारा लागू किया जा सकता करने के लिए एक तरह से की जरूरत है। This विभिन्न चीजों का एक एसएल डेमो है लेकिन डेटा साझा करने के लिए एक सिंगलटन इंजेक्शन दे रहा है, वही विचार wpf में लागू होता है।

ShellView:

<Window x:Class="CMWPFGuardSample.ShellView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Grid Background="White"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <StackPanel Grid.Row="0" 
        Orientation="Horizontal"> 
      <Button x:Name="Open" 
        Content="Open" /> 
      <Button x:Name="Settings" 
        Content="Settings" /> 
      <Button x:Name="Results" 
        Content="Results" /> 
     </StackPanel> 
    </Grid> 

</Window> 

ShellViewModel:

[Export(typeof (IShell))] 
    public class ShellViewModel : PropertyChangedBase, IShell 
    { 
     private bool _isOpen; 
     public bool IsOpen 
     { 
      get { return _isOpen; } 
      set 
      { 
       _isOpen = value; 
       NotifyOfPropertyChange(() => IsOpen); 
       NotifyOfPropertyChange(() => CanSettings); 
      } 
     } 

     private bool _isSettings; 
     public bool IsSettings 
     { 
      get { return _isSettings; } 
      set 
      { 
       _isSettings = value; 
       NotifyOfPropertyChange(() => IsSettings); 
       NotifyOfPropertyChange(() => CanResults); 
      } 
     } 

     public bool IsResults { get; set; } 

     public void Open() 
     { 
      IsOpen = true; 
     } 

     public bool CanSettings 
     { 
      get { return IsOpen; } 
     } 

     public void Settings() 
     { 
      IsSettings = true; 
     } 

     public bool CanResults 
     { 
      get { return IsSettings; } 
     } 

     public void Results() 
     { 
     } 
    } 
0

एमवीवीएम और डब्ल्यूपीएफ कमांड ICommand.CanExecute() विधि में निर्मित होने के बाद से आपकी "मुश्किल भाग" आवश्यकताओं को पूरी तरह से फिट करता है जो कस्टम तर्क के आधार पर संबंधित बटन को सक्षम/अक्षम करने की अनुमति देता है।

इस नाइस सुविधा का उपयोग करने के लिए पहले RoutedCommand Class पर एक नज़र डालें और एमएसडीएन How to: Enable a Command पर स्वयं व्याख्यात्मक उदाहरण देखें (नीचे कोड स्निपेट देखें)।

और सामान्य रूप से एमवीवीएम के बारे में, यह वास्तव में सरल है! बस इसे कोशिश करते हैं और आप इसे बिना नहीं छोड़ देंगे;) कुछ शब्दों में - आप प्रत्येक EntityView.xaml इसी EntityViewModel वर्ग के लिए बनाने के लिए और फिर बस देखें के DataContext में इसके बारे में उदाहरण डाल या तो स्पष्ट कोड में या बाइंडिंग का उपयोग कर:

var entityViewModel = new EntityViewModel(); 
var view = new EntityView(); 
view.DataContext = entityViewModel; 

MVVM कमान और Command.CanExecute बाइंडिंग:

XAML:

<Window x:Class="WCSamples.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="CloseCommand" 
    Name="RootWindow" 
    > 
    <Window.CommandBindings> 
    <CommandBinding Command="ApplicationCommands.Close" 
        Executed="CloseCommandHandler" 
        CanExecute="CanExecuteHandler" 
        /> 
    </Window.CommandBindings> 
    <StackPanel Name="MainStackPanel"> 
    <Button Command="ApplicationCommands.Close" 
      Content="Close File" /> 
    </StackPanel> 
</Window> 

सी # के पीछे कोड:

// Create ui elements. 
StackPanel CloseCmdStackPanel = new StackPanel(); 
Button CloseCmdButton = new Button(); 
CloseCmdStackPanel.Children.Add(CloseCmdButton); 

// Set Button's properties. 
CloseCmdButton.Content = "Close File"; 
CloseCmdButton.Command = ApplicationCommands.Close; 

// Create the CommandBinding. 
CommandBinding CloseCommandBinding = new CommandBinding(
    ApplicationCommands.Close, CloseCommandHandler, CanExecuteHandler); 

// Add the CommandBinding to the root Window. 
RootWindow.CommandBindings.Add(CloseCommandBinding); 
+0

आदर्श नहीं। कैलिब्रॉन माइक्रो सभी आईसीओएमएंडएफ़ गफ से बचाता है। http://caliburnmicro.codeplex.com/discussions/250844 –

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