{"id":220,"date":"2016-07-06T17:19:39","date_gmt":"2016-07-06T08:19:39","guid":{"rendered":"http:\/\/kanehiro.exe.jp\/?p=220"},"modified":"2016-07-06T17:24:22","modified_gmt":"2016-07-06T08:24:22","slug":"xamarin%e3%80%81%e3%82%b6%e3%83%9e%e3%83%aa%e3%83%b3%e3%80%81%e9%9d%92%e7%a9%ba%e3%80%81%e6%99%b4%e3%82%8c%e3%81%9f%e7%a9%ba%e3%80%80%ef%bc%8dxamarin%e3%81%a7%e6%ac%a1%e4%b8%96%e4%bb%a3mobile%e7%89%88","status":"publish","type":"post","link":"https:\/\/kanehiro.exe.jp\/?p=220","title":{"rendered":"Xamarin\u3001\u30b6\u30de\u30ea\u30f3\u3001\u9752\u7a7a\u3001\u6674\u308c\u305f\u7a7a\u3000\uff0dXamarin\u3067\u6b21\u4e16\u4ee3mobile\u7248\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9Realm\u3092\u4f7f\u3046\uff0d"},"content":{"rendered":"<p>\u3000\u3053\u3061\u3089\u306e<a href=\"http:\/\/www.kerorin.com\/fanclub\/cm.html\" title=\"\u30b1\u30ed\u30ea\u30f3\" target=\"_blank\">\u30b5\u30a4\u30c8<\/a>\u306e\u30e1\u30ed\u30c7\u30a3\u30fc\u3068\u3068\u3082\u306b\u304a\u697d\u3057\u307f\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u3000Realm\u306f\u3001SQLite+ORM\u306b\u5909\u308f\u308b\u30e2\u30d0\u30a4\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u3059\u3002Realm\u306f\u3001NoSQL\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\uff08\u975e\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\uff09\u3067\u3059\u306e\u3067\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\u3057\u3066\u624b\u8efd\u306b\u6c38\u7d9a\u5316\u3067\u304d\u307e\u3059\u3002<br \/>\n\u3000\u73fe\u5728\u3001Realm\u306fAndroid\u3068iOS\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u306e\u3067\u3001Xamarin\u3067\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<a href=\"http:\/\/www.buildinsider.net\/mobile\/xamarintips\/0050\" target=\"_blank\">Build Insider\u306b\u63b2\u8f09\u3055\u308c\u3066\u3044\u308bXamarin\u3067SQLite\u3092\u4f7f\u3046\u30b3\u30f3\u30d1\u30af\u30c8\u306a\u30b5\u30f3\u30d7\u30eb<\/a>\u3068\u540c\u69d8\u306a\u30a2\u30d7\u30ea\u3092Xamarin.Forms Portable\u3067\u4f5c\u6210\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\n\u3000\u307e\u305a\u3001Visual Studio\u3067Xamarin.Forms Portable\u306a\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n<a href=\"http:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm1-300x187.png\" alt=\"useRealm1\" width=\"300\" height=\"187\" class=\"alignnone size-medium wp-image-221\" srcset=\"https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm1-300x187.png 300w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm1-1024x638.png 1024w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm1-624x389.png 624w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm1.png 1687w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n\u4f5c\u6210\u3057\u305f\u3089\u3001NuGet\u30d1\u30c3\u30b1\u30fc\u30b8\u30de\u30cd\u30fc\u30b8\u30e3\u3067\u3001realm\u3092\u691c\u7d22\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<br \/>\n\u3000<br \/>\n\u3000\u6700\u521d\u306bModel\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u307e\u3059\u3002\u30b0\u30eb\u30fc\u30d7\u540d\u3060\u3051\u3092\u8a18\u9332\u3059\u308bGroup.cs\u3067\u3059\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nusing Realms;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace UseRealmApp\r\n{\r\n    public class Group : RealmObject\r\n    {\r\n        &#x5B;ObjectId]\r\n        public string ID { get; set; }\r\n        public string Name { get; set; }\r\n    }\r\n}\r\n<\/pre>\n<p>\u3000Realm\u306b\u306f\u3044\u308f\u3086\u308b\u30aa\u30fc\u30c8\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u306a\u5024\u3092\u632f\u308b\u6a5f\u80fd\u306f\u3042\u308a\u307e\u305b\u3093\u306e\u3067\u3001ID\u3082String\u306b\u3057\u3066GUID\u5024\u3092\u8a18\u9332\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3059\u3002<br \/>\n\u3000\u6b21\u304e\u306b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u64cd\u4f5c\u3059\u308bMemoDatabase\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u5c06\u6765\u7684\u306bGroup\u30af\u30e9\u30b9\u3060\u3051\u3067\u306a\u304f\u3001\u30e1\u30e2\u3092\u8a18\u9332\u3059\u308b\u30af\u30e9\u30b9\u306a\u3069\u3092\u8ffd\u52a0\u3057\u3066\u3044\u304f\u306e\u3067MemoDatabase\u3068\u3044\u3046\u30af\u30e9\u30b9\u540d\u306b\u3057\u307e\u3057\u305f\u3002<br \/>\n\u3000GetGroups()\u30e1\u30bd\u30c3\u30c9\u306fGroup\u30af\u30e9\u30b9\u306e\u5168\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002GetGroups()\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3092ListView\u306b\u8868\u793a\u3057\u307e\u3059\u3002GetGroup\u306f\u6307\u5b9a\u3057\u305fID\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002\u4ee5\u964d\u306e\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u306f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u4e0b\u306b\u89e3\u8aac\u3092\u8a18\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nusing Realms;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace UseRealmApp\r\n{\r\n    public class MemoDatabase\r\n    {\r\n        Realm realm;\r\n        public MemoDatabase()\r\n        {\r\n            realm = Realm.GetInstance();\r\n        }\r\n        public IEnumerable&lt;Group&gt; GetGroups()\r\n        {\r\n            return realm.All&lt;Group&gt;().ToList();\r\n        }\r\n        public Group GetGroup(string id)\r\n        {\r\n            return realm.All&lt;Group&gt;()\r\n                        .ToList()\r\n                        .FirstOrDefault(x =&gt; x.ID == id);\r\n        }\r\n        public string SaveGroup(Group group)\r\n        {\r\n            var newGroup = realm.All&lt;Group&gt;()\r\n                             .ToList()\r\n                             .FirstOrDefault(x =&gt; x.ID == group.ID);\r\n\r\n            using (var trans = realm.BeginWrite())\r\n            {\r\n                if (newGroup == null)\r\n                {\r\n                    newGroup = realm.CreateObject&lt;Group&gt;();\r\n                    newGroup.ID = Guid.NewGuid().ToString();\r\n                }\r\n\r\n                newGroup.Name = group.Name;\r\n \r\n                trans.Commit();\r\n            }\r\n\r\n            return newGroup?.ID;    \/\/ ? Nullable\u578b\r\n        }\r\n        public string DeleteGroup(string id)\r\n        {\r\n            var item = realm.All&lt;Group&gt;()\r\n                             .ToList()\r\n                             .FirstOrDefault(x =&gt; x.ID == id);\r\n\r\n            if (item == null)\r\n                return null;\r\n\r\n            using (var trans = realm.BeginWrite())\r\n            {\r\n                realm.Remove(item);\r\n                trans.Commit();\r\n            }\r\n\r\n            return id;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u3000SaveGroup\u30e1\u30bd\u30c3\u30c9\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002using\u3067\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u3092\u56f2\u307f\u307e\u3059\u3002DeleteGroup\u30e1\u30bd\u30c3\u30c9\u306f\u6307\u5b9a\u3057\u305fID\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u524a\u9664\u3057\u307e\u3059\u3002<\/p>\n<p>\u3000app.cs\u3067\u30da\u30fc\u30b8\u3092\u8868\u793a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing Xamarin.Forms;\r\n\r\nnamespace UseRealmApp\r\n{\r\n    public class App : Application\r\n    {\r\n        public App()\r\n        {\r\n            MainPage = new MyPage();\r\n        }\r\n    }\r\n    class MyPage : ContentPage\r\n    {\r\n\r\n        readonly MemoDatabase _db = new MemoDatabase();\r\n\r\n        public MyPage()\r\n        {\r\n\r\n            var listView = new ListView\r\n            {\r\n                ItemsSource = _db.GetGroups(), \r\n                ItemTemplate = new DataTemplate(typeof(TextCell)) \r\n            };\r\n            listView.ItemTemplate.SetBinding(TextCell.TextProperty, &quot;Name&quot;);\r\n            listView.ItemTapped += async (s, a) =&gt; {\r\n                var group = (Group)a.Item;\r\n                if (await DisplayAlert(&quot;\u524a\u9664\u3057\u3066\u3088\u308d\u3057\u3044\u3067\u3059\u304b&quot;, group.Name, &quot;OK&quot;, &quot;\u30ad\u30e3\u30f3\u30bb\u30eb&quot;))\r\n                {\r\n                    _db.DeleteGroup(group.ID); \/\/ group\u306e\u524a\u9664\r\n                    listView.ItemsSource = _db.GetGroups(); \/\/ \u30ea\u30b9\u30c8\u306e\u66f4\u65b0\r\n                }\r\n            };\r\n            var entry = new Entry\r\n            { \r\n                HorizontalOptions = LayoutOptions.FillAndExpand\r\n            };\r\n            var buttonAdd = new Button\r\n            {\r\n                WidthRequest = 60,\r\n                Text = &quot;\u8ffd\u52a0&quot;\r\n            };\r\n            buttonAdd.Clicked += (s, a) =&gt; { \r\n                if (!String.IsNullOrEmpty(entry.Text))\r\n                { \/\/ Entry\u306b\u6587\u5b57\u5217\u304c\u5165\u529b\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306b\u4fdd\u5b58\r\n                    var group = new Group { Name = entry.Text };\r\n                    _db.SaveGroup(group);\r\n                    listView.ItemsSource = _db.GetGroups(); \/\/\u30ea\u30b9\u30c8\u306e\u66f4\u65b0\r\n                    entry.Text = &quot;&quot;;\r\n                }\r\n            };\r\n\r\n            Content = new StackLayout\r\n            { \r\n                Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0),\r\n                Children = {\r\n                    new StackLayout {\r\n                        Padding = 5,\r\n                        Orientation = StackOrientation.Horizontal,\r\n                        Children = {entry, buttonAdd} \r\n                    },\r\n                    listView \r\n                }\r\n            };\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u3000ListView\u306b\u306f\u30b0\u30eb\u30fc\u30d7\u540d\u3092\u4e00\u89a7\u8868\u793a\u3057\u3001\u30bf\u30c3\u30d7\u3057\u305f\u3089\u524a\u9664\u78ba\u8a8d\u306e\u30c0\u30a4\u30a2\u30ed\u30b0\u3092\u8868\u793a\u3057\u307e\u3059\u3002<br \/>\n<a href=\"http:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm2-168x300.png\" alt=\"useRealm2\" width=\"168\" height=\"300\" class=\"alignnone size-medium wp-image-222\" srcset=\"https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm2-168x300.png 168w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm2-576x1024.png 576w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm2-624x1109.png 624w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm2.png 1440w\" sizes=\"(max-width: 168px) 100vw, 168px\" \/><\/a><br \/>\n\u3000Entry\u306b\u30b0\u30eb\u30fc\u30d7\u540d\u3092\u5165\u529b\u3057\u3066\u8ffd\u52a0\u30dc\u30bf\u30f3\u3092\u30bf\u30c3\u30d7\u3057\u305f\u3089\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u8a18\u9332\u3057\u3066ListView\u3092\u66f4\u65b0\u3057\u307e\u3059\u3002<br \/>\n<a href=\"http:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm3-168x300.png\" alt=\"useRealm3\" width=\"168\" height=\"300\" class=\"alignnone size-medium wp-image-223\" srcset=\"https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm3-168x300.png 168w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm3-576x1024.png 576w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm3-624x1109.png 624w, https:\/\/kanehiro.exe.jp\/wp-content\/uploads\/2016\/07\/useRealm3.png 1440w\" sizes=\"(max-width: 168px) 100vw, 168px\" \/><\/a><br \/>\n\u3000Xamarin\u3067\u3082Realm\u3092\u7c21\u5358\u306b\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u3053\u3061\u3089\u306e\u30b5\u30a4\u30c8\u306e\u30e1\u30ed\u30c7\u30a3\u30fc\u3068\u3068\u3082\u306b\u304a\u697d\u3057\u307f\u304f\u3060\u3055\u3044\u3002 \u3000Realm\u306f\u3001SQLite+ORM\u306b\u5909\u308f\u308b\u30e2\u30d0\u30a4\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u3059\u3002Realm\u306f\u3001NoSQL\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\uff08\u975e\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\uff09\u3067\u3059\u306e\u3067\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,12,13],"tags":[],"_links":{"self":[{"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=\/wp\/v2\/posts\/220"}],"collection":[{"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=220"}],"version-history":[{"count":0,"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kanehiro.exe.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}