2014-07-10 5 views
16

जब मैं XAML के माध्यम से एक कमांड पर अपना बटन वायर करता हूं, तो मुझे रन टाइम त्रुटि सिस्टम मिल रहा है। Windows.Markup.XamlParseException: 'System.Windows.Data.Binding' पर मान प्रदान करें अपवाद फेंक दिया। ---> System.InvalidCastException: 'System.Reflection.MethodInfo' टाइप करने के लिए 'System.Reflection.RuntimeEventInfo' प्रकार की ऑब्जेक्ट डालने में असमर्थ।कमांड बाध्यकारी 'System.Reflection.MethodInfo' टाइप करने के लिए 'System.Reflection.RuntimeEventInfo' प्रकार की ऑब्जेक्ट डालने में असमर्थ

Click="{Binding ElementName=MainGrid, Path=DataContext.AlertClickCommand}" 

कोड दृश्य मॉडल (मेरी खिड़की के पीछे तार की कोड में:

जब मैं XAML हर चीज में बाध्यकारी आदेश को दूर ठीक से काम करता है, और मेरे आइटम प्रदर्शित आदि .. यहाँ आदेश बाध्यकारी है):

this.AlertsView.DataContext = GlobalStuff.AlertManager1.AlertViewModel1; 

यहाँ मेरे विचार मॉडल है मेरा विचार मॉडल मेरे विचार से डेटा संदर्भ है

using System.Collections.Generic; 
using System.ComponentModel; 
using Arkle.SharedUI.Model; 
using Arkle.SharedUI.ViewModel.Commands; 

namespace Arkle.SharedUI.ViewModel 
{ 
    public class AlertViewModel : INotifyPropertyChanged 
    { 

     private List<Alert> _alerts = new List<Alert>(); 
     public List<Alert> Alerts 
     { 
      get { return _alerts; } 
      set 
      { 
       _alerts = value; 
       OnPropertyChanged("Alerts"); 
      } 
     } 


     public AlertViewModel() 
     { 
      if (DesignerProperties.IsInDesignMode) 
      { 
       LoadDesignTimeData(); 
      } 
     } 

     private void LoadDesignTimeData() 
     { 
      Alerts.Add(new Alert { BackgroundMessage = "Sis", IsAlerting = true, OverlayMessage = "3 mins", Tip = "Sis Data not received for 3 mins" }); 
      Alerts.Add(new Alert { BackgroundMessage = "Bets", IsAlerting = true, OverlayMessage = "4", Tip = "4 unchecked danger bets" }); 
      Alerts.Add(new Alert { BackgroundMessage = "Texts", IsAlerting = false, OverlayMessage = "3", Tip = "3 Unchecked Text Bets" }); 
     } 

     private AlertClickCommand _alertClickCommand; 
     public AlertClickCommand AlertClickCommand 
     { 
      get { return _alertClickCommand ?? (_alertClickCommand = new AlertClickCommand(this)); } 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

यहाँ मेरी XAML है

<UserControl x:Class="Arkle.SharedUI.View.AlertsView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:viewModel="clr-namespace:Arkle.SharedUI.ViewModel" 
     mc:Ignorable="d" 
     d:DataContext="{d:DesignInstance Type=viewModel:AlertViewModel, IsDesignTimeCreatable=True}" 
     x:Name="EarlyPriceEditorViewModelWindow" 
    Height="Auto" Width="Auto"> 
    <UserControl.Resources> 
     <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> 
    </UserControl.Resources> 

    <Grid Name="MainGrid"> 
     <StackPanel Name="MainStackPanel"> 
      <ListBox ItemsSource="{Binding Path=Alerts}" > 
       <ListBox.ItemsPanel> 
        <ItemsPanelTemplate> 
         <WrapPanel Orientation="Horizontal" > 
         </WrapPanel> 
        </ItemsPanelTemplate> 
       </ListBox.ItemsPanel> 

       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal" Visibility="{Binding IsAlerting,Converter={StaticResource BooleanToVisibilityConverter}}"> 
          <StackPanel Orientation="Horizontal"> 
           <Button Content="{Binding BackgroundMessage}" HorizontalAlignment="Left" Width="75" VerticalAlignment="Top" Height="Auto" Margin="2" 
            Click="{Binding ElementName=MainGrid, Path=DataContext.AlertClickCommand}" CommandParameter="{Binding}" 

           /> 
           <Label Content="{Binding OverlayMessage}" HorizontalAlignment="Left" Width="Auto" Margin="1,0,0,0" VerticalAlignment="Top" Background="Red" Foreground="White" 
            FontWeight="Bold"> 
            <Label.Style> 
             <Style> 
              <Style.Triggers> 
               <DataTrigger Binding="{Binding IsAlerting}" Value="True"> 
                <Setter Property="Image.Visibility" Value="Visible" /> 
                <DataTrigger.EnterActions> 
                 <BeginStoryboard x:Name="ImageFlash"> 
                  <Storyboard> 
                   <DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)" 
               BeginTime="0:0:0" Duration="0:0:0.5" 
               From="1.0" To="0.0" RepeatBehavior="Forever" AutoReverse="True"/> 
                  </Storyboard> 
                 </BeginStoryboard> 
                </DataTrigger.EnterActions> 
                <DataTrigger.ExitActions> 
                 <StopStoryboard BeginStoryboardName="ImageFlash" /> 
                </DataTrigger.ExitActions> 
               </DataTrigger> 
               <DataTrigger Binding="{Binding IsAlerting}" Value="False"> 
                <DataTrigger.Setters> 
                 <Setter Property="Image.Visibility" Value="Collapsed" /> 
                </DataTrigger.Setters> 
               </DataTrigger> 
              </Style.Triggers> 
             </Style> 

            </Label.Style> 
           </Label> 
           <Label Content="|" Margin="5"/> 
          </StackPanel> 


         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 

      </ListBox> 


     </StackPanel> 
    </Grid> 

</UserControl> 

यहाँ मेरी आदेश

using System; 
using System.Windows.Input; 
using Arkle.Common; 
using Arkle.SharedUI.Events; 
using Arkle.SharedUI.Model; 

namespace Arkle.SharedUI.ViewModel.Commands 
{ 
    public class AlertClickCommand : ICommand 
    { 
     private AlertViewModel _alertViewModel; 
     public AlertClickCommand(AlertViewModel alertViewModel) 
     { 
      _alertViewModel = alertViewModel; 
     } 

     public void Execute(object parameter) 
     { 
      if (parameter == null) 
      { 
       return; 
      } 
      var parameterAsAlert = (Alert)parameter; 

      switch (parameterAsAlert.BackgroundMessage) 
      { 
       case "Bets": 
        EventManager.Instance.GetEvent<ShowDangerBetsRequestedEvent>().Publish(null); 
        break; 
       default: 
        return; 
      } 
     } 

     public bool CanExecute(object parameter) 
     { 
      return true; 
     } 

     public event EventHandler CanExecuteChanged; 
    } 
} 
है

मैं भी निम्नलिखित डिजाइन समय त्रुटि मिलती है (स्क्रीनशॉट देखें) 'प्रकार की वस्तु कास्ट करने में असमर्थ System.Windows.Data.Binding 'टाइप करने के लिए' Microsoft.Expression.Markup.DocumentModel.DocumentNode '।

enter image description here

पहले रनटाइम त्रुटि - भागो फेंकता enter image description here

बाद क्रम त्रुटियों थ्रो बार-बार: System.Windows.Markup.XamlParseException: 'System.Windows.Data पर मूल्य प्रदान करें। बाध्यकारी 'एक अपवाद फेंक दिया। ---> System.InvalidCastException: 'System.Reflection.MethodInfo' टाइप करने के लिए 'System.Reflection.RuntimeEventInfo' प्रकार की ऑब्जेक्ट डालने में असमर्थ।

System.Windows.Data.BindingBase.ProvideValue (IServiceProvider ServiceProvider)

पर MS.Internal.Helper.CheckCanReceiveMarkupExtension (MarkupExtension markupExtension, IServiceProvider ServiceProvider, DependencyObject & targetDependencyObject, DependencyProperty & targetDependencyProperty)

पर

पर MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue (मार्कअप एक्सटेंशन मुझे, IServiceProvider सेवाप्रदाता)

--- आंतरिक अपवाद स्टैक ट्रेस का अंत ---

System.Windows.FrameworkTemplate.LoadTemplateXaml (XamlReader templateReader, XamlObjectWriter currentWriter)

पर

.......................

+0

कहाँ और आप अपने 'DataContext' कैसे निर्धारित किया है? आपका व्यू मॉडल कोड कहां है (कम से कम संपत्ति)? और भलाई के लिए, कृपया उस हास्यास्पद रूप से लंबे स्टैक ट्रेस को हटाएं, जब तक कि आप उपयोगकर्ताओं को अपना प्रश्न पढ़ने से रोकने का इरादा नहीं रखते। – Sheridan

+0

हाय, मैंने डेटाकॉन्टेक्स्ट को सेट करने के लिए प्रश्न में संशोधन किया है। – DermFrench

उत्तर

30

आप Click संपत्ति पर कोई आदेश बाध्य नहीं करते हैं।Click संपत्ति पारंपरिक घटना हैंडलर को Click ईवेंट में जोड़ने के लिए है। आप अपने आदेश को बांधने के लिए Command प्रॉपर्टी का उपयोग करना चाहते हैं।

<Button Content="{Binding BackgroundMessage}" 
     HorizontalAlignment="Left" Width="75" 
     VerticalAlignment="Top" Height="Auto" Margin="2" 
     Command="{Binding ElementName=MainGrid, 
          Path=DataContext.AlertClickCommand}" 
     CommandParameter="{Binding}" /> 
+3

धन्यवाद, मैं मूर्ख हूँ !!! – DermFrench

+16

यह सिर्फ 'चेकबॉक्स' के लिए मुझे खुशी है ... मैं 'Ischecked' के बजाय 'चेक किया गया' के लिए बाध्यकारी था और समस्या के लिए कहीं और चारों ओर देखा जब तक कि मैं अपवाद पर नजदीकी नज़र डालता हूं, Google पर जाता हूं और यहां उतरा :) * 2016 में अभी भी उपयोगी * – grek40

7

यह कैसे XAML परिवर्तन के बारे में:

<Button Content="{Binding BackgroundMessage}" 
     HorizontalAlignment="Left" Width="75" 
     VerticalAlignment="Top" Height="Auto" Margin="2" 
     Click="{Binding ElementName=MainGrid,     
         Path=DataContext.AlertClickCommand}" 
     CommandParameter="{Binding}" /> 
इस के लिए

:

<Button Content="{Binding BackgroundMessage}" 
     HorizontalAlignment="Left" Width="75" 
     VerticalAlignment="Top" Height="Auto" Margin="2" 
     Command="{Binding ElementName=MainGrid,       
          Path=DataContext.AlertClickCommand}" 
     CommandParameter="{Binding}" /> 
+0

पहले जवाब दोहराएं - लेकिन मैंने देखा कि इसे दूसरे के बाद केवल 3 मिनट पोस्ट किया गया था, इसलिए संभवतः आप पोस्ट करने की प्रक्रिया में थे जब दूसरे आदमी ने आपको मार दिया, और इसलिए मैं डाउनवोट नहीं होगा, लेकिन इसके बजाय, ऊपर की ओर :) – vapcguy

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

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