xamarinでsqlserverに接続し取得した値を画面表示するということをしたいため、DependencyServiceを使い、PCLにインターフェースを作り、Androidコード側MainActivity.csに接続コードを実装し、PCLのMainPage.xaml.csでメソッド呼び出しをしてMainPage.xamlで表示というコードを書いていますが、データ受け渡しでうまくいかず配置エラーが起きてしまいます。
ツール:Visual Studio 2017 Community
PCLインターフェース
public interface IPlatformInfoInterface { // モデル名を取得するメソッド string GetDBtable();
MainActivity.cs
var textView = FindViewById<TextView>(Resource.Id.textView1); public string GetDBtable() { SqlCommand command = new SqlCommand(sql, con); sqlReader = command.ExecuteReader(); while (sqlReader.Read()) { textView.Text += sqlReader[0].ToString() + ":" + sqlReader[1].ToString() + System.Environment.NewLine; }
MainPage.xaml.cs
InitializeComponent(); // DependencyServiceから、IPlatformInfoオブジェクトを取得する IPlatformInfoInterface platformInfo = DependencyService.Get<IPlatformInfoInterface>(); // 取得したオブジェクトを使う textView.textView1 = platformInfo.GetDBtable(); }
MainPage.xaml
<Label x:Name="textView1" text="" VerticalOptions="Center" HorizontalOptions="Center" />
テーブルデータは列項目2つ
列1 | 列2
A | B
C | D
E | F
G | H
・
・
・
Insertされている分だけ画面に表示します。
そもそもDependencyServiceなど使わず、delegateやバインディングなどでこんなやり方があるよなどもしあればアドバイス頂きたいです。
回答1件
あなたの回答
tips
プレビュー