c# – wp7芒果中的DataTemplateSelector

c# – wp7芒果中的DataTemplateSelector,第1张

概述我的应用程序中需要一个数据模板选择器.我发现 this blog在7.0上运行得很好. 将项目升级到7.1后,在设置模板时出现“未指定的错误”. 我试图将他们在博客上发布的示例项目升级到芒果,它仍然有效.我不知道我做错了什么,因为看起来这个代码对于芒果来说是好的. 有什么建议? <Grid x:Name="LayoutRoot" Background="Transparent" VerticalA 我的应用程序中需要一个数据模板选择器.我发现 this blog在7.0上运行得很好.
将项目升级到7.1后,在设置模板时出现“未指定的错误”.
我试图将他们在博客上发布的示例项目升级到芒果,它仍然有效.我不知道我做错了什么,因为看起来这个代码对于芒果来说是好的.
有什么建议?

<GrID x:name="LayoutRoot" Background="transparent" VerticalAlignment="Stretch" >    <StackPanel VerticalAlignment="Stretch">        <StackPanel OrIEntation="Horizontal">            <TextBlock x:name="nameBlock" GrID.Row="0" GrID.Column="0" VerticalAlignment="Center" Text="IDentifIEr:" />            <TextBox x:name="nameTextBox" GrID.Row="0" GrID.Column="1" Text="{Binding Path=IDentifIEr,Mode=TwoWay}" Height="72" WIDth="410" TextChanged="nameTextBox_TextChanged"/>        </StackPanel>        <StackPanel GrID.Row="3" GrID.Column="1" Background="Black" VerticalAlignment="Stretch" >            <ListBox margin="12,12,0" name="ListBox1" Background="transparent" ItemsSource="{Binding Path=PropertIEsCollection}" VerticalAlignment="Stretch" Height="300" >                <ListBox.ItemTemplate>                    <DataTemplate>                        <viewmodel:ParameterTemplateSelector Content="{Binding}">                            <viewmodel:ParameterTemplateSelector.TextDataTemplate>                                <DataTemplate>                                    <GrID>                                        <GrID.ColumnDeFinitions>                                            <ColumnDeFinition WIDth="100"></ColumnDeFinition>                                            <ColumnDeFinition WIDth="*"></ColumnDeFinition>                                        </GrID.ColumnDeFinitions>                                        <TextBlock Text="{Binding Key}" VerticalAlignment="Center" GrID.Column="0" />                                        <TextBox name="propertyTextBox" Text="{Binding Value,Mode=TwoWay}" GrID.Column="1" WIDth="400" TextChanged="propertyTextBox_TextChanged" />                                    </GrID>                                </DataTemplate>                            </viewmodel:ParameterTemplateSelector.TextDataTemplate>                            <viewmodel:ParameterTemplateSelector.NumberDataTemplate>                                <DataTemplate>                                    <GrID>                                        <GrID.ColumnDeFinitions>                                            <ColumnDeFinition WIDth="100"></ColumnDeFinition>                                            <ColumnDeFinition WIDth="*"></ColumnDeFinition>                                        </GrID.ColumnDeFinitions>                                        <TextBlock Text="{Binding Key}" VerticalAlignment="Center" GrID.Column="0" />                                        <TextBox name="propertyTextBox" Text="{Binding Value,Mode=TwoWay}" GrID.Column="1" WIDth="400" TextChanged="propertyTextBox_TextChanged" />                                    </GrID>                                </DataTemplate>                            </viewmodel:ParameterTemplateSelector.NumberDataTemplate>                            <viewmodel:ParameterTemplateSelector.DateDataTemplate>                                <DataTemplate>                                    <GrID>                                        <GrID.ColumnDeFinitions>                                            <ColumnDeFinition WIDth="100"></ColumnDeFinition>                                            <ColumnDeFinition WIDth="*"></ColumnDeFinition>                                        </GrID.ColumnDeFinitions>                                        <TextBlock Text="{Binding Key}" VerticalAlignment="Center" GrID.Column="0" />                                        <toolkit:DatePicker  GrID.Column="1" WIDth="400" Value="{Binding Value,Mode=TwoWay}" ValueChanged="DatePicker_ValueChanged"/>                                    </GrID>                                </DataTemplate>                            </viewmodel:ParameterTemplateSelector.DateDataTemplate>                            <viewmodel:ParameterTemplateSelector.TimeDataTemplate>                                <DataTemplate>                                    <GrID>                                        <GrID.ColumnDeFinitions>                                            <ColumnDeFinition WIDth="100"></ColumnDeFinition>                                            <ColumnDeFinition WIDth="*"></ColumnDeFinition>                                        </GrID.ColumnDeFinitions>                                        <TextBlock Text="{Binding Key}" VerticalAlignment="Center" GrID.Column="0" />                                        <toolkit:TimePicker GrID.Column="1" WIDth="400" Value="{Binding Value,Mode=TwoWay}" ValueChanged="TimePicker_ValueChanged"/>                                    </GrID>                                </DataTemplate>                            </viewmodel:ParameterTemplateSelector.TimeDataTemplate>                        </viewmodel:ParameterTemplateSelector>                    </DataTemplate>                </ListBox.ItemTemplate>            </ListBox>        </StackPanel>    </StackPanel></GrID>
public class ParameterTemplateSelector : DataTemplateSelector{    public DataTemplate TextDataTemplate    {        get;        set;    }    public DataTemplate NumberDataTemplate    {        get;        set;    }    public DataTemplate DateDataTemplate    {        get;        set;    }    public DataTemplate TimeDataTemplate    {        get;        set;    }    public DataTemplate PictureDataTemplate    {        get;        set;    }    public DataTemplate NonParamDataTemplate    {        get;        set;    }    public overrIDe DataTemplate SelectTemplate(object item,DependencyObject container)    {        string type = "";        if (item is IParam)        {            IParam parameter = item as IParam;            type = parameter.Type;        }        else if (item is keyvalue)        {            keyvalue k = item as keyvalue;            type = k.Type;        }        switch (type)        {            case "String":                return TextDataTemplate;            case "Text":                return TextDataTemplate;            case "Number":                return NumberDataTemplate;            case "Date":                return DateDataTemplate;            case "Time":                return TimeDataTemplate;            case "Picture":                return PictureDataTemplate;            default:                return TextDataTemplate;        }    }}    public abstract class DataTemplateSelector : ContentControl{    public virtual DataTemplate SelectTemplate(object item,DependencyObject container)    {        return null;    }    protected overrIDe voID OnContentChanged(object oldContent,object newContent)    {        base.OnContentChanged(oldContent,newContent);        ContentTemplate = SelectTemplate(newContent,this);    }}

我得到的例外是:

System.Exception occurred  Message=UnspecifIEd error   StackTrace:   at MS.Internal.Xcpimports.CheckHResult(UInt32 hr)   at MS.Internal.Xcpimports.FrameworkElement_MeasureOverrIDe(FrameworkElement element,Size availableSize)   at System.windows.FrameworkElement.MeasureOverrIDe(Size availableSize)   at System.windows.FrameworkElement.MeasureOverrIDe(IntPtr nativeTarget,Double inWIDth,Double inHeight,Double& outWIDth,Double& outHeight)   at MS.Internal.Xcpimports.MeasureOverrIDeNative(IntPtr element,Single inWIDth,Single inHeight,Single& outWIDth,Single& outHeight)   at MS.Internal.Xcpimports.FrameworkElement_MeasureOverrIDe(FrameworkElement element,Double& outHeight)   at MS.Internal.Xcpimports.MeasureNative(IntPtr element,Single inHeight)   at MS.Internal.Xcpimports.UIElement_Measure(UIElement element,Size availableSize)   at System.windows.UIElement.Measure(Size availableSize)   at System.windows.Controls.VirtualizingStackPanel.MeasureChild(UIElement child,Size layoutSlotSize)   at System.windows.Controls.VirtualizingStackPanel.MeasureOverrIDe(Size constraint)   at System.windows.FrameworkElement.MeasureOverrIDe(IntPtr nativeTarget,Size availableSize)   at System.windows.FrameworkElement.MeasureOverrIDe(Size availableSize)   at System.windows.Controls.ScrollContentPresenter.MeasureOverrIDe(Size constraint)   at System.windows.FrameworkElement.MeasureOverrIDe(IntPtr nativeTarget,Size availableSize)   at System.windows.UIElement.Measure(Size availableSize)   at System.windows.Controls.ScrollVIEwer.MeasureOverrIDe(Size constraint)   at System.windows.FrameworkElement.MeasureOverrIDe(IntPtr nativeTarget,Size availableSize)   at System.windows.FrameworkElement.MeasureOverrIDe(Size availableSize)   at Microsoft.Phone.Controls.PivotItem.MeasureOverrIDe(Size availableSize)   at System.windows.FrameworkElement.MeasureOverrIDe(IntPtr nativeTarget,Size availableSize)   at System.windows.FrameworkElement.MeasureOverrIDe(Size availableSize)   at Microsoft.Phone.Controls.Pivot.MeasureOverrIDe(Size availableSize)   at System.windows.FrameworkElement.MeasureOverrIDe(IntPtr nativeTarget,Double& outHeight)   at MS.Internal.Xcpimports.UpdateLayoutNative(IntPtr element)   at MS.Internal.Xcpimports.UIElement_UpdateLayout(UIElement element)   at System.windows.UIElement.UpdateLayout()   at Microsoft.Phone.Controls.Pivot.OnItemsChanged(NotifyCollectionChangedEventArgs e)   at System.windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)   at System.windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)   at System.windows.Controls.ItemCollection.UpdateItemsSourceList(IEnumerable newItemsSource)   at System.windows.Controls.ItemsControl.ItemsSourceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)   at System.windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp,Object oldValue,Object newValue)   at System.windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property,EffectiveValueEntry oldEntry,EffectiveValueEntry& newEntry,ValueOperation operation)   at System.windows.DependencyObject.RefreshExpression(DependencyProperty dp)   at System.windows.Data.BindingExpression.RefreshExpression()   at System.windows.Data.BindingExpression.SendDataToTarget()   at System.windows.Data.BindingExpression.sourceAcquired()   at System.windows.Data.BindingExpression.System.windows.IDataContextChangedListener.OnDataContextChanged(Object sender,DataContextChangedEventArgs e)   at System.windows.Data.BindingExpression.DataContextChanged(Object sender,DataContextChangedEventArgs e)   at System.windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)   at System.windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)   at System.windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)   at System.windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)   at System.windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)   at System.windows.FrameworkElement.OnPropertyChanged(DependencyProperty dp)   at System.windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp,ValueOperation operation)   at System.windows.DependencyObject.SetValueInternal(DependencyProperty dp,Object value,Boolean allowReadonlySet)   at System.windows.DependencyObject.SetValueInternal(DependencyProperty dp,Object value)   at System.windows.DependencyObject.SetValue(DependencyProperty dp,Object value)   at System.windows.FrameworkElement.set_DataContext(Object value)   at WPUserControls.viewmodel.ItemsTypeviewmodel.AddItem(ItemInstance itemInstance)   at WPUserControls.viewmodel.ItemsTypePageviewmodel.AddNewItemInstance()   at WPUserControls.VIEws.ItemsTypePageVIEw.AddItem()   at WPUserControls.VIEws.ItemsTypePageVIEw.addIcon_Click(Object sender,EventArgs e)   at Microsoft.Phone.Shell.ApplicationbarItemContainer.FireEventHandler(EventHandler handler,Object sender,EventArgs args)   at Microsoft.Phone.Shell.ApplicationbarIconbutton.ClickEvent()   at Microsoft.Phone.Shell.ApplicationbarIconbuttonContainer.ClickEvent()   at Microsoft.Phone.Shell.Applicationbar.OnCommand(UInt32 IDCommand)   at Microsoft.Phone.Shell.Interop.NativeCallbackInteropWrapper.OnCommand(UInt32 IDCommand)
解决方法 我无法确定问题,但似乎问题不在你的模板选择器本身,而是模板中的一个.

尝试将它们中的每一个减少到最小基数(不同颜色的文本框)或其他东西,看它是否有效,还尝试添加其他类型的项目,看看会发生什么.

总结

以上是内存溢出为你收集整理的c# – wp7芒果中的DataTemplateSelector全部内容,希望文章能够帮你解决c# – wp7芒果中的DataTemplateSelector所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1233905.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存