0

これら3つのクラスで構成されるWP Class Library 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 オブジェクトを構築できない場合、エミュレータはそのように動作します。質問から、アプリケーションからコードへの参照は1つしかありません。Resources の 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);
    }
}

1行目でOKです。2行目でOKです。パス "C:\Stuff\WindowsPhone\AppLearningHowTo2\AppLearningHowTo2\DAL\DB.xml" は Windows Phone では受け入れられないため、3 行目で例外が発生します。アクセスできるすべてのファイルは、XAP のコンテンツ、アセンブリ内のリソース、および分離ストレージ内のファイルです。

次の記事が役立つかもしれませんWP7 分離ストレージのすべて - テキスト ファイルの読み取りと保存

于 2012-07-01T13:59:03.893 に答える