0

我创建了一个由这三个类组成的 WP 类库BusinessLogic项目

1)

namespace BusinessLogic
{
    public class Bottle : INotifyPropertyChanged
    {
        // Due to INotifyPropertyChanged interface
        public event PropertyChangedEventHandler PropertyChanged;

        // Proprietà Title
        private string title;
        public string Title
        {
            set
            {
                if (title != value)
                {
                    title = value;
                    OnPropertyChanged("Title");
                }
            }
            get
            {
                return title;
            }
        }
        // Proprietà PhotoFileName
        private string photoFileName;
        public string PhotoFileName
        {
           set
            {
                if (photoFileName != value)
                {
                    photoFileName = value;
                    OnPropertyChanged("PhotoFileName");
                }
            }
            get
            {
                return photoFileName;
            }
        } 
    protected virtual void OnPropertyChanged(string propChanged)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propChanged));
    }
  }
}

2)瓶子

namespace BusinessLogic
{
    public class Bottles : INotifyPropertyChanged
    {
        // Due to INotifyPropertyChanged interface
        public event PropertyChangedEventHandler PropertyChanged;

        // Proprietà MainTitle
        private string mainTitle;
    public string MainTitle
    {
        set
        {
            if (mainTitle != value)
            {
                mainTitle = value;
                OnPropertyChanged("MainTitle");
            }
        }
        get
        {
            return mainTitle;
        }
    }

    // Proprietà bottles
    private ObservableCollection<Bottle> bottleSet = new ObservableCollection<Bottle>();
    public ObservableCollection<Bottle> BottleSet
    {
        set
        {
            if (bottleSet != value)
            {
                bottleSet = value;
                OnPropertyChanged("BottleSet");
            }
        }
        get { return bottleSet; }
    }

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

}
}

3) BottlesPresenter

namespace BusinessLogic
{
    public class BottlesPresenter : INotifyPropertyChanged
    {
    // Due to INotifyPropertyChanged interface
    public event PropertyChangedEventHandler PropertyChanged;


    // Proprietà BottleMatrix
    private Bottles bottlesMatrix;
    public Bottles BottlesMatrix
    {
        protected set
        {
            if (bottlesMatrix != value)
            {
                bottlesMatrix = value;
                OnPropertyChanged("BottlesMatrix");
            }
        }
        get { return bottlesMatrix; }
    }

    public BottlesPresenter()
    {
        XmlSerializer xml = new XmlSerializer(typeof(Bottles));

        using (StreamReader fileReader = new         StreamReader(@"C:\Stuff\WindowsPhone\AppLearningHowTo2\AppLearningHowTo2\DAL\DB.xml"))
        {
            BottlesMatrix = (Bottles)xml.Deserialize(fileReader);
        }
    }

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

BottlePresenter 构造函数应该从位于文件系统中的 xml 文件反序列化。它包含以下标签

<?xml version="1.0"?>
<Bottles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MainTitle>MainTitle</MainTitle>
  <Bottleset>
    <Bottle>
      <Title>Title1</Title>
      <PhotoFileName>PhotoFileName1</PhotoFileName>
    </Bottle>
    <Bottle>
      <Title>Title2</Title>
      <PhotoFileName>PhotoFileName2</PhotoFileName>
    </Bottle>
  </Bottleset>
</Bottles>

然后我创建了一个 WP 应用程序,并引用了BusinessLogic.dll项目库。

在 MainPage.xaml 文件中,我放置了 XML 命名空间声明

xmlns:businesslogic="clr-namespace:BusinessLogic;assembly=BusinessLogic"

然后我在 MainPage.xaml Resources 集合中实例化了BottlesPresenter类

<phone:PhoneApplicationPage.Resources>
    <businesslogic:BottlesPresenter x:Key="bottlesPresenter" />        
</phone:PhoneApplicationPage.Resources>

最后在内容区域中放置一个 TextBlock 并绑定到该资源:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock HorizontalAlignment="Center"
         VerticalAlignment="Center"
         Text="{Binding Source={StaticResource bottlesPresenter},
                                  Path=Bottles.MainTitle}" />

不幸的是,我启动了调试器,打开了模拟器,到达了启动画面并且没有继续。

简而言之:我无法创建BottlesPresenter类的实例。

我把头撞在墙上好几个星期都没有找到解决办法。

请问有人可以帮我吗?

非常感谢

安东尼奥

4

1 回答 1

0

当 WP7 无法构造 Application 对象时,模拟器的行为类似。从问题来看,我只看到 Application 对您的代码的 1 个引用。它是资源中的 BottlePresenter。XamlLoader 尝试创建此类型的实例。

查看您的 BottlePresenter 构造器中的内容:

public BottlesPresenter()
{
    XmlSerializer xml = new XmlSerializer(typeof(Bottles));

    using (StreamReader fileReader = new StreamReader(
    @"C:\Stuff\WindowsPhone\AppLearningHowTo2\AppLearningHowTo2\DAL\DB.xml"))
    {
        BottlesMatrix = (Bottles)xml.Deserialize(fileReader);
    }
}

第一行没问题。第二行没问题。第三行导致异常,因为路径“C:\Stuff\WindowsPhone\AppLearningHowTo2\AppLearningHowTo2\DAL\DB.xml”在 Windows Phone 上是不可接受的。您可以访问的所有文件是 XAP 的内容、程序集中的资源以及独立存储中的文件。

以下文章可能会对您有所帮助关于 WP7 隔离存储 - 读取和保存文本文件

于 2012-07-01T13:59:03.893 回答