xamarin.formsプログラムについて質問です。
環境はwin10 visual studio2017
データベース接続するInterfaceとDBアクションと項目名のgetsetを作成してPCL化し、
SampleApp.csから参照している状態です。
(参考:Xamarin エキスパート養成読本)
下記エラーが出ています。
ーーーーーーーーーーーーーーーーーーーー
Unhandled Exception:
System.InvalidOperationException: Cannot convert "2017/08/●● ●●:●●" into Xamarin.Forms.Color
このデバッグが止まっている箇所はMainActivity.csの23行目LoadApplicationになります。詳細は下記です。
ーーーーーーーーーーーーーーーーーーーー
デバッグ中の出力は以下です。
ーーーーーーーーーーーーーーーーーー
08-25 15:03:04.285 D/Mono (32120): Image addref System.Runtime.Serialization[0x9c94b040] -> System.Runtime.Serialization.dll[0x9c92d600]: 2
08-25 15:03:04.285 D/Mono (32120): Prepared to set up assembly 'System.Runtime.Serialization' (System.Runtime.Serialization.dll)
08-25 15:03:04.286 D/Mono (32120): Assembly System.Runtime.Serialization[0x9c94b040] added to domain RootDomain, ref_count=1
08-25 15:03:04.289 D/Mono (32120): AOT: image 'System.Runtime.Serialization.dll.so' not found: dlopen failed: library "/data/app/SampleApp.Android-1/lib/arm/libaot-System.Runtime.Serialization.dll.so" not found
08-25 15:03:04.291 D/Mono (32120): AOT: image '/usr/local/lib/mono/aot-cache/arm/System.Runtime.Serialization.dll.so' not found: dlopen failed: library "/data/app/SampleApp.Android-1/lib/arm/libaot-System.Runtime.Serialization.dll.so" not found
08-25 15:03:04.291 D/Mono (32120): Config attempting to parse: 'System.Runtime.Serialization.dll.config'.
08-25 15:03:04.291 D/Mono (32120): Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Runtime.Serialization/System.Runtime.Serialization.config'.
08-25 15:03:04.291 D/Mono (32120): Assembly Ref addref Mono.Android[0xaf37be80] -> System.Runtime.Serialization[0x9c94b040]: 2
08-25 15:03:04.294 D/Mono (32120): Assembly Ref addref System.Runtime.Serialization[0x9c94b040] -> mscorlib[0xaf37b4c0]: 37
ーーーーーーーーーーーーーーーーーー
ハンドルされていないエラーとしてデバッグを始めた直後にデバッグが止まってしまい、
googleなどで調べても解決できない状態です。書籍問い合わせもしていますが、いっこうにお返事なく
何かアドバイス頂けないでしょうか。
よろしくお願いいたします。
ご指摘アドバイス頂きましてPCL側なども記載します。
●全体的な構成
下記のようにPCLプロジェクトを作成
![イ(f4755f61160ee8cac59a3204fcf6cd25.png)
そのPCLを参照しているSampleAppプロジェクトを作成
●主要なコード
PCL側
・SamplePCL.cs
namespace SamplePCL
{
public class SamplePCL : ContentPage
{
public SamplePCL()
{
var button = new Button
{
Text = "Click Me!",
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
int clicked = 0; button.Clicked += (s, e) => button.Text = "Clicked: " + clicked++; Content = button; } }
}
・SqliteControl.cs
using System.Collections.Generic;
using SQLite.Net;
using Xamarin.Forms;
namespace SampleApp
{
public class SqliteControl
{
static readonly object wObject = new object();
readonly SQLiteConnection wSQLiteConnection;
// SQLiteデータベースに接続
public SqliteControl()
{
//接続
wSQLiteConnection =
DependencyService.Get<ISqliteInterface>().GetConnection();
// データ管理用テーブル作成
wSQLiteConnection.CreateTable<SqliteItem>();
} // データの取得 public IEnumerable<SqliteItem> GetItems() { lock (wObject) { return wSQLiteConnection.Table<SqliteItem>(); } } // データの登録 public void InsertItem(SqliteItem item) { lock (wObject) { wSQLiteConnection.Insert(item); } } // データの削除 public void DeleteItem(SqliteItem item) { lock (wObject) { wSQLiteConnection.Delete(item); } } }
}
・SqliteInterface.cs
using SQLite.Net;
namespace SampleApp
{
public interface ISqliteInterface { SQLiteConnection GetConnection(); }
}
・SqliteItem.cs
using SQLite.Net.Attributes;
namespace SampleApp
{
public class SqliteItem
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Text { get; set; }
public DateTime InsertDate { get; set; }
}
}
画面プロジェクト側
・AppSaml.cs
using Xamarin.Forms;
namespace SampleApp
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new SampleApp.SamplePage(); }
略
}
・SamplePage.cs
using System;
using Xamarin.Forms;
using SQLite.Net;
using SamplePCL;
namespace SampleApp
{
class SamplePage : ContentPage
{
readonly SqliteControl wSqliteControl = new SqliteControl();
public SamplePage()
{
// データ表示用リスト
var wListView = new ListView
{
ItemsSource = wSqliteControl.GetItems(),
ItemTemplate = new DataTemplate(typeof(TextCell))
};
wListView.ItemTemplate.SetBinding(TextCell.TextProperty, "Text");
wListView.ItemTemplate.SetBinding(TextCell.DetailColorProperty, new Binding("InsertDate",
stringFormat: "{0:yyy/MM/dd hh:mm}"));
// データタップ時の処理
wListView.ItemTapped += async (s, a) =>
{
var wItem = (SqliteItem)a.Item;
if (await DisplayAlert("削除しますか", wItem.Text, "はい", "いいえ"))
{
// データの削除
wSqliteControl.DeleteItem(wItem);
// データの再取得
wListView.ItemsSource = wSqliteControl.GetItems();
}
};
// ラベル1
var wLabel1 = new Label
{
Text = "サンプルアプリ (データ登録・削除)",
BackgroundColor = Color.Navy,
TextColor = Color.White,
WidthRequest = 600
};
// ラベル2
var wLabel2 = new Label
{
Text = "登録データ一覧",
BackgroundColor = Color.Gray,
TextColor = Color.White,
WidthRequest = 300
};
// データ入力
var wEntry = new Entry
{
HorizontalOptions = LayoutOptions.FillAndExpand
};
// 登録ボタン
var wButtonIns = new Button
{
Text = "登録",
TextColor = Color.White,
WidthRequest = 60
};
// 登録ボタンの押下時の処理
wButtonIns.Clicked += (s, a) =>
{
if (!String.IsNullOrEmpty(wEntry.Text))
{
// 登録データ設定
var item = new SqliteItem
{
Text = wEntry.Text,
InsertDate = DateTime.Now
};
// データの登録
wSqliteControl.InsertItem(item);
// データの再取得
wListView.ItemsSource = wSqliteControl.GetItems();
// 入力データ初期化
wEntry.Text = "";
}
};
// 画面
Content = new StackLayout
{
Padding = new Thickness
(
0,
Device.OnPlatform(20, 0, 0),
0,
0
),
Children =
{
wLabel1,
new StackLayout
{
BackgroundColor = Color.Green,
Padding = 5,
Orientation = StackOrientation.Horizontal,
Children =
{
wEntry, wButtonIns
}
},
wLabel2,
wListView
}
};
}
}
}
画面プロジェクトのAndroid側
MainActivity.cs
namespace SampleApp.Droid
{
[Activity(Label = "SampleApp", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle); global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App()); } }
}
その後考えたのですが、
http://ticktack.hatenablog.jp/entry/2017/02/06/234012
非推奨なのでエラーの直接的な原因なのかは定かではないのですが、
この入れ方でもしかしたらうまくいくかもしれないと考えてはいますが、breakの前に数字を置いてもうまくいかずといった状況が今です。
他のアプローチなどアドバイスありましたらよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/25 07:15
2017/08/27 11:00
2017/08/27 11:02
2017/08/28 00:05
2017/08/28 00:51
2017/09/01 20:18