2017年1月28日土曜日

Xamarin.Forms 最初のラベルを固定、以下をスクロールしたい

元ネタ
https://forums.xamarin.com/discussion/20945/scrollview-in-xaml

<StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" Spacing="0">

      <Label x:Name="lblHeader" Text="商品詳細" VerticalOptions="Center" HorizontalOptions="Center" FontSize="24"
               TextColor="White" BackgroundColor="Blue" />

      <ScrollView Orientation="Vertical" VerticalOptions="FillAndExpand">
        <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand">


最初にStackLayoutを配置、Orienrationは当然、Vertical
その後、ヘッダとして固定したい部品を配置する。

それから、再度、StackLayoutを重ね、ScrollViewを配置する。

2017年1月20日金曜日

Xamrin.Forms WCFからPickerにData bindingする。

Xamrin.Forms WCFからPickerにData bindingする。

ViewModelクラスに、データ取得ロジックを記述する。

        public async Task<List<string>> Get商品区分() {
            var list商品区分 = new List<string>();

            var Recs = await _wcfServiceWrapper.GetProductGroupAsync(2);

            foreach (var item in Recs) {
                list商品区分.Add(item.DisplayName);
            }

            _list商品区分 = list商品区分;   //内部でSelectedIndex値に対してのvalueを保持する

            return list商品区分;
        }


XAMLページのコードビハインド側で、XAMLにデータをバインドを行う。

    public partial class MonthPage : ContentPage {
        public MonthPage() {
            InitializeComponent();
            BindingContext = App.Locator.Month;
        }

        protected async override void OnAppearing() {   //画面表示の時
            base.OnAppearing();

            pic商品区分.ItemsSource = await App.Locator.Month.Get商品区分();
        }
    }


Xamarin.Forms + MVVMLight + WCFの組み合わせ

誰かもっといい方法があるなら、教えてください。

2017年1月19日木曜日

EntityFrameWorkからレコード取得

            var records = db.vDeban_Henko
              .Where(x => x.ID == Convert.ToInt64(Session["UserID"]))
              .OrderBy(x => x.Hizuke).OrderBy(y => y.SHUBETSU).OrderBy(z => z.JOKYO)
              .ToList();

EntityFrameWorkで複数項目のソート

            var records = db.vDeban_Henko
              .Where(x => x.ID == Convert.ToInt64(Session["UserID"]))
              .OrderBy(x => x.Hizuke).OrderBy(y => y.SHUBETSU).OrderBy(z => z.JOKYO)
              .ToList();

2017年1月18日水曜日

Xamarin.Forms + MVVMLightでダイアログを表示する

Xamarin.Forms + MVVMLightでダイアログを表示する。

MVVMで、ViewModelからViewを制御して、ダイアログを表示する。

簡単なサンプルを表示する。


XAMLを使っているので、Pageのコードビハインドに、Notificationの受け口を設定
DisplayAlert を使って、ダイアログを表示している。

using GalaSoft.MvvmLight.Messaging;

namespace AskaShop.View {
    public partial class LoginPage : ContentPage {
        public LoginPage() {
            InitializeComponent();
            BindingContext = App.Locator.Login;

            // Messenger 経由で ViewModel から UI 操作を要求されたとき行う処理を登録しておく。
            Messenger.Default.Register<NotificationMessage>(this, this.Alert);
        }

        public void Alert(NotificationMessage message) {
            DisplayAlert("", message.Notification, "OK");
        }
    }
}


ViewModel側のコード

using GalaSoft.MvvmLight.Messaging;
namespace AskaShop.ViewModel {
    public class LoginViewModel : ViewModelBase {
        #region local variable
        private readonly IWcfServiceWrapper _wcfServiceWrapper;

        private bool _isCallingWebservice;

        private string _entUserId;
        private string _entPassword;
        private string _lblBuild;
        private string _btnLoginColor;
        #endregion

        public LoginViewModel(IWcfServiceWrapper wcfServiceWrapper) {

            if (wcfServiceWrapper == null) throw new ArgumentNullException("wcfServiceWrapper");
            _wcfServiceWrapper = wcfServiceWrapper;

            SubmitCommand = new RelayCommand(GetResponseAsync, () => CanGetResponse);
            CanGetResponse = true;
            IsCallingWebservice = false;
            btnLoginColor = "Blue";
            lblBuild = "Build 17-01-14/18";
        }

        private async void GetResponseAsync() {

            CanGetResponse = false;      //ボタンを無効
            IsCallingWebservice = true;  //処理中の表示
            btnLoginColor = "#1e90ff";   //ボタンの背景色をdodgerblueへ変更

            if (_entUserId == null || _entUserId.Trim() == "") {
                CanGetResponse = true;
                IsCallingWebservice = false;
                btnLoginColor = "Blue";

                // Messenger 経由で View に DisplayAlert の表示を要求する。
                MessengerInstance.Send(new NotificationMessage("ユーザーIDが未入力です。"));
                return;
            }

            if (_entPassword == null || _entPassword.Trim() == "") {
                CanGetResponse = true;
                IsCallingWebservice = false;
                btnLoginColor = "Blue";

                // Messenger 経由で View に DisplayAlert の表示を要求する。
                MessengerInstance.Send(new NotificationMessage("パスワードが未入力です。"));
                return;
            }

            var vUser = await _wcfServiceWrapper.GetUser_MstAsync(_entUserId, _entPassword);

            if (vUser.USERID == null) {
                CanGetResponse = true;
                IsCallingWebservice = false;
                btnLoginColor = "Blue";

                // Messenger 経由で View に DisplayAlert の表示を要求する。
                MessengerInstance.Send(new NotificationMessage("ユーザーIDもしくは、パスワードが間違っています。"));
                return;
            }

            //global変数の編集
            App.UserId         = vUser.USERID;
            App.UserName       = vUser.USER名;
            App.得意先Cd       = vUser.得意先CD;
            App.得意先名       = vUser.得意先名;
            App.メールアドレス = vUser.メールアドレス;
            App.得意先単価GRP  = vUser.得意先単価GRP;
            App.カレンダー区分 = vUser.カレンダー区分;
            App.事業所Cd       = vUser.事業所Cd;
            App.営業所Cd       = vUser.営業所Cd;

            //画面遷移
            App.Current.MainPage = new EbiShop.View.MenuPage();
        }
    }
}

ViewModel側は、 MessengerInstance.Send(new NotificationMessage("パスワードが未入力です。"));

だけでOKだった。

Xamarin.Forms WCFでエラー発生 Specified value has invalid CRLF characters

Xamarin.Forms WCFでエラー発生。
発生したエラーは  Specified value has invalid CRLF characters.

英語では、WCFから返すXMLに invalid CRLF
間違ったCRLFが入っているとなっているが、WCFTestClientの返すXMLを見る限り
XMLとして、間違っている箇所は無かった。

もしかして、送るXMLは漢字OKだけど、返すXMLって、WCF Client側になるから
漢字ってNGかな? と試しに、漢字を無くすと、あっさりOK。

WCFの受け取り側は、Windows のIISだから漢字OKだったのね。
WCF Client側は、今回、PCLだったので、漢字NGだったようです。

WCF ClientをWPFで作成して、単体テストを行うと、問題なし。

Stack Over Flowにも、事例は無し あっても、.NETのVersionUPしろみたいな記事だった。

上記のエラーのおかげで、一旦は、Xamarin + WCFの組み合わせをやめようと思い
Xamarin + REST のサンプルソースを見ると、WCFより断然ソース量が多い。

ソース量が少ない、WCFに戻りました(軟弱.....)

Xamarin.Forms PCLには、漢字を使わないように気を付けます。



2017年1月13日金曜日

Xamarin.Formsで画像を表示する

Xamarin.Formsで画像を表示する

imgLogo.Source = ImageSource.FromResource("EbiShop.Images.logo.png"); //の会社ロゴを指定



アプリケーション名.フォルダ名.ファイル名

これは、PCLの場合の指定方法です。



Xamarin.FormsのWCF Clientは非同期で受け取る。

Xamarin.FormsのWCF Clientは非同期で受け取る。

WCFのの戻り値に awaitを入れなかったから、非同期にならなくて
上手く画面が更新されなかった。

インターフェース
Task<string> GetDataAsync(int number);

実装部
 public async Task<string> GetDataAsync(int number) {
            Task<string> getDataTask = new TaskFactory().FromAsync<int, string>(_wcfService.BeginGetData, _wcfService.EndGetData, number, null, TaskCreationOptions.None);
            return await getDataTask;
        }

return の次に、 awaitのおまじないが必要でした。

2017年1月12日木曜日

MVVM Light toolkitのMessengerでメッセージ送信時に結果を受け取る

MVVMLight toolkitのMessengerでメッセージ送信時に結果を受け取る。

sampleはコチラ
MVVM-Light-toolkitMessenger

sampleがあると、助かります。