gpt4 book ai didi

wpf - 设计时错误(未找到可附加属性 x)

转载 作者:行者123 更新时间:2023-12-02 18:33:27 25 4
gpt4 key购买 nike

我使用 Blend for VS 2012 创建了 WP7.8 应用程序的模板。有一些示例数据(位于单独的 xaml 文件中,见下文),这些数据在 silverlight 页面上的设计模式中正确显示。更改此数据(使用命名空间和类名)会导致编辑器错误,并且新数据不再显示在设计模式中。 (List组件显示为空)

页面的主要xaml文件如下

<phone:PhoneApplicationPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
x:Class="DesignSketch.MainPage"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Pivot Control-->
<controls:Pivot Title="Application">
<!--Pivot item one-->
<controls:PivotItem Header="List">
<!--Double line list with text wrapping-->
<ListBox x:Name="FirstListBox" ItemsSource="{Binding Items}" Margin="0,0,-12,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding Sum}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>


</controls:Pivot>
</Grid>
</phone:PhoneApplicationPage>

可以看出它使用了

d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"

用于设计模式绑定(bind)。但是,错误(提示)显示为“在 MainViewModelSampleData.xaml 中发现错误

这个xaml的内容是

<viewModels2:ExpensesPageVM
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels2="clr-namespace:DesignSketch"
>
<viewModels2:ExpensesPageVM.Items>
<viewModels2:Expense Sum="10" Description="Fee1" />
<viewModels2:Expense Sum="600" Description="Fee2" />
</viewModels2:ExpensesPageVM.Items>

</viewModels2:ExpensesPageVM>

显示以下提示错误
在 ExpensesPageVM 类型中找不到可附加属性 Items

类型 viewModels2:Expense 未找到。验证您没有缺少程序集引用并且所有引用的程序集均已构建

ExpensesPageVM 和 Expense 类都位于 DesignSke​​tch 命名空间中,并具有以下代码:

using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace DesignSketch
{
public class ExpensesPageVM : INotifyPropertyChanged
{
public ExpensesPageVM()
{
this.Items = new ObservableCollection<Expense>();
}

/// <summary>
/// A collection for ItemViewModel objects.
/// </summary>
public ObservableCollection<Expense> Items { get; set; }

private string _sampleProperty = "Sample Runtime Property Value";
/// <summary>
/// Sample ViewModel property; this property is used in the view to display its value using a Binding
/// </summary>
/// <returns></returns>
public string SampleProperty
{
get
{
return _sampleProperty;
}
set
{
_sampleProperty = value;
NotifyPropertyChanged("SampleProperty");
}
}

public bool IsDataLoaded
{
get;
private set;
}

/// <summary>
/// load initial data from sdf
/// </summary>
public void LoadData()
{
//...
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

其他文件

using System;
using System.Data.Linq.Mapping;
using System.ComponentModel;

namespace DesignSketch
{
[Table]
public class Expense: INotifyPropertyChanged, INotifyPropertyChanging
{
private int expenseID;

[Column(IsPrimaryKey = true,
IsDbGenerated = true,
DbType="INT NOT NULL Identity",
CanBeNull=false,
AutoSync=AutoSync.OnInsert)]
public int ExpenseID { get {return expenseID;}
set
{
if (expenseID == value) return;
NotifyPropertyChanging("ExpenseID");
expenseID = value;
NotifyPropertyChanged("ExpenseID");
}
}

private string description { get; set; }

[Column]
public string Description { get { return description; }
set
{
if (description == value) return;

NotifyPropertyChanging("Description");
description = value;
NotifyPropertyChanged("Description");
}
}

private decimal sum;

[Column]
public decimal Sum
{
get { return sum; }
set
{
if (sum == value) return;

NotifyPropertyChanging("Sum");
sum = value;
NotifyPropertyChanged("Sum");

}
}

private DateTime date;

[Column]
public DateTime Date { get { return date; }
set
{
if (date == value) return;

NotifyPropertyChanging("Date");
date = value;
NotifyPropertyChanged("Date");
}
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

public event PropertyChangingEventHandler PropertyChanging;

private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
}
}

费用被定制为存储在手机的本地数据库中。

我实际上有相同的代码工作(由 Blend 生成),其中示例数据在设计时完美显示。有人知道为什么这段代码不存在吗?

最佳答案

如果 DesignSke​​tch 不在当前程序集中,则更改此行

    xmlns:viewModels2="clr-namespace:DesignSketch"

    xmlns:viewModels2="clr-namespace:DesignSketch;assembly=NameOfAssemblyThatHoldsDesignSketch"

关于wpf - 设计时错误(未找到可附加属性 x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15782036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com