public HomePage() { this.InitializeComponent(); this.DataContext = App.ViewModel; this._viewModel = App.ViewModel; Window.Current.SizeChanged += (s, e) => { ResizeContentWidth(); }; }
/// <summary> /// 単一アプリケーション オブジェクトを初期化します。これは、実行される作成したコードの ///最初の行であるため、main() または WinMain() と論理的に等価です。 /// </summary> public App() { Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync( Microsoft.ApplicationInsights.WindowsCollectors.Metadata | Microsoft.ApplicationInsights.WindowsCollectors.Session); this.InitializeComponent(); this.Suspending += OnSuspending; this.Resuming += OnResuming; StateManager = new AppStateManager(); StateManager.StateList.Add(AppState.Mobile, 0); StateManager.StateList.Add(AppState.Normal, 800); StateManager.StateList.Add(AppState.Wide, 1600); ViewModel = new MainViewModel(); var vm = DataLoadAsync().Result; if (vm != null) { ViewModel = vm; } else { ViewModel.InitializeCommand.Execute(null); } ViewModel.OnRegisterVoiceCommand += async (xml) => { try { var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync("VoiceCommandFile", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, xml); await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(file); await DataSaveAsync(); var dialog = new MessageDialog("Cortanaの更新が完了しました"); await dialog.ShowAsync(); } catch (Exception e) { var dialog = new MessageDialog(e.Message, "Cortanaの更新に失敗しました"); await dialog.ShowAsync(); } }; OnChangeAppState += (s,s2) => { }; }
public async void Run(IBackgroundTaskInstance taskInstance) { this.serviceDeferral = taskInstance.GetDeferral(); try { var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null && triggerDetails.Name == "CortanaCommandService") { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); var voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); Debug.WriteLine(voiceCommand.CommandName); MainViewModel viewModel = new MainViewModel(); var vm = await DataLoadAsync(); if (vm != null) { viewModel = vm; } var cols = voiceCommand.CommandName.Split('_'); var commandName = cols[0]; var stateName = cols[1]; var commandViewModel = viewModel.CommandList.First(q => q.Name == commandName); commandViewModel.CurrentStateNum++; var stateViewModel = commandViewModel.StateList.ElementAt(commandViewModel.CurrentStateNum - 1); if (commandViewModel.CurrentStateNum>=commandViewModel.StateList.Count) { commandViewModel.CurrentStateNum = 0; } if(stateViewModel is SuccessStateViewModel) { var state = stateViewModel as SuccessStateViewModel; if (string.IsNullOrEmpty(state.Utterance)) { state.Utterance = ""; } var message = new VoiceCommandUserMessage(); message.SpokenMessage = state.Utterance; message.DisplayMessage = state.Utterance; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); } else if(stateViewModel is ScriptStateViewModel) { var state = stateViewModel as ScriptStateViewModel; if (!string.IsNullOrEmpty(state.Script)) { try { ConnectionData connectionData = new ConnectionData(); connectionData.AcceptPass = viewModel.PassCode; connectionData.Script = state.Script.Replace("\n", ";").Replace("\r", "").Replace("\t", ""); string json = JsonConvert.SerializeObject(connectionData); var byteData = Encoding.UTF8.GetBytes(json); StreamSocket socket = new StreamSocket(); await socket.ConnectAsync(new HostName("127.0.0.1"), SettingManager.ServerPort); var writer = new DataWriter(socket.OutputStream); writer.WriteBytes(byteData); await writer.StoreAsync(); await writer.FlushAsync(); writer.Dispose(); socket.Dispose(); } catch (Exception) { var errorMsg = new VoiceCommandUserMessage(); string msg = "スクリプトの実行を試みましたがサーバーが起動してませんでした"; errorMsg.SpokenMessage = msg; errorMsg.DisplayMessage = msg; var errorResponse = VoiceCommandResponse.CreateResponse(errorMsg); await voiceServiceConnection.ReportFailureAsync(errorResponse); return; } } if (string.IsNullOrEmpty(state.Utterance)) { state.Utterance = ""; } var message = new VoiceCommandUserMessage(); message.SpokenMessage = state.Utterance; message.DisplayMessage = state.Utterance; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); } await DataSaveAsync(viewModel); } }catch(Exception e) { var message = new VoiceCommandUserMessage(); message.SpokenMessage = "何かしらのエラーが起きました"; message.DisplayMessage = e.Message; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); var toast = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01); ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toast)); } this.serviceDeferral.Complete(); }
private async Task DataSaveAsync(MainViewModel viewModel) { var json = JsonConvert.SerializeObject(viewModel, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync(SettingManager.ViewModelDataFileName, CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, json); }
public PreviewPage() { this.InitializeComponent(); this.DataContext = App.ViewModel; this._viewModel = App.ViewModel; }
private async void OnResuming(object sender, object e) { var vm = await DataLoadAsync(); if(vm != null) { App.ViewModel = vm; } }