埋め込みリソースの使い方で、マイクロソフトのサンプルでわからないことが出てきました
埋め込みリソースを使おうと思い、
https://docs.microsoft.com/ja-jp/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows
こちらのページからサンプルをダウンロードして、埋め込みリソースを使えるようにはなったのですが、
理解出来ないことが出てきました
サンプルだと
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LoadResourceText)).Assembly; Stream stream = assembly.GetManifestResourceStream("WorkingWithFiles.LibJsonResource.json"); Earthquake[] earthquakes; using (var reader = new StreamReader(stream)) { var json = reader.ReadToEnd(); var rootobject = JsonConvert.DeserializeObject<Rootobject>(json); earthquakes = rootobject.earthquakes;
としてJsonファイルを読んでいるのですが
この中のアセンブリタイプをLoadResourceTextを指定しているのですが
このクラスが以下のクラスで、ただテキストを読んできて画面表示しているだけのクラスで何故このクラスを指定するのかわからないです
public LoadResourceText () { var editor = new Label { Text = "loading...", HeightRequest = 300}; #region How to load a text file embedded resource var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LoadResourceText)).Assembly; Stream stream = assembly.GetManifestResourceStream("WorkingWithFiles.LibTextResource.txt"); string text = ""; using (var reader = new StreamReader (stream)) { text = reader.ReadToEnd (); } #endregion editor.Text = text; Content = new StackLayout { Margin = new Thickness(20), VerticalOptions = LayoutOptions.StartAndExpand, Children = { new Label { Text = "Embedded Resource Text File", FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)), FontAttributes = FontAttributes.Bold }, editor } }; }
試しに自分で以下のようにアセンブリの元になるクラスを空っぽで作って、埋め込みリソースをリードする関数を作ってみたところ、ちゃんと動作しました
IntrospectionExtensions.GetTypeInfo
に指定するクラスはどういう考え方で作れば良いのでしょう(動けば良いだけというのなら空っぽのダミークラスで良いのでしょうか)
//空っぽのクラスを作る internal class LoadResourceText { } //アセンブリタイプに空っぽのクラスを指定 public void GetEmbedResource() { var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LoadResourceText)).Assembly; Stream stream = assembly.GetManifestResourceStream("TestXamarinJson.TextFile1.txt"); string text = ""; using (var reader = new StreamReader(stream)){ text = reader.ReadToEnd(); } }
あなたの回答
tips
プレビュー