crossframe » XAML http://crossframe.iiv.jp Windows Dev. Site Tue, 07 Nov 2023 06:31:52 +0000 ja hourly 1 https://wordpress.org/?v=3.8.41 C++/CLI + XAML ../../../20130503227/ ../../../20130503227/#comments Fri, 03 May 2013 04:07:55 +0000 http://xfra.me/?p=227 前々回のC++/CX + XAML に比較して、C++/CLIでもできるのかどうかテストしたところ、一応(形の上では?)できたので備忘録としておきたいと思います。
(WebViewもトライしてみましたができませんでした。方法はあるのかな?)

環境 : VisualStudio2012 / Windows 8
プロジェクトは、C++ CLRの空のプロジェクトを選びました。

using namespace System;
using namespace System::IO;
using namespace System::Windows;
using namespace System::Windows::Markup;
using namespace System::Windows::Navigation;
using namespace System::Threading; 

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	FileStream^ fs = gcnew FileStream("xamltest.xaml", FileMode::Open, FileAccess::Read);
	Window^ win =  safe_cast<Window^>( XamlReader::Load(fs) );

	if(win != nullptr){
		win->Show();
	}
	Thread::Sleep(1000);

    return 0;
}

xamltest.xaml

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
	Title="XamlTest" Height="300" Width="500">
	<Grid>
		<Label VerticalAlignment="Center" HorizontalAlignment="Center">Xaml Test</Label>
	</Grid>
</Window>

実行すると、1秒間だけ以下の画面が表示されます。

xamtest

]]>
../../../20130503227/feed/ 0
C++/CX + XAML WebViewでストアアプリ ../../../20130427201/ ../../../20130427201/#comments Sat, 27 Apr 2013 13:15:23 +0000 http://xfra.me/?p=201 Windowsプラットホームの開発でいろいろと気になったことをテストしてきましたが、いよいよWindowsストアアプリの開発をしようということで、いろいろと準備を始めました。(本当はWindows Phone 8アプリの開発をやりたかったのですが、端末がないのでそれまではストアアプリでやれることはやっておこうと・・)
しかしこのストアアプリ、どんな言語で開発したらいいか、WindowsPhoneアプリへの移行がスムースのスタイルは、過去のライブラリを使う方法等、選択肢がいろいろとある分迷ってしまいます。JavaScript+HTML5かC#+XAMLか、と迷っていたらXAMLでWebViewを使う方法があると知ったので、テストしてみました。

環境: VisualStudio2012 / Windows8

新しいプロジェクトで、Visual C++ -> Windowsストア -> 新しいアプリケーション(XAML) を選びました。
(C#でもいいのですが、新しいC++/CXにも触れたかったので)

MainPage.xaml

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <WebView x:Name="WebView1" ScriptNotify="wv_notify" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,88,0,0"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="40,28,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
        <TextBox x:Name="TextBox1" HorizontalAlignment="Left" Margin="166,28,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="38" Width="363" TextChanged="TextBox_TextChanged_1"/>
</Grid>

MainPage.xaml.cpp

void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
	String^ html = "<html><head><script type='text/javascript'>\n" +
		"function jsFunc1(){ document.getElementById('test1').innerText = 'Hello!';}\n" +
		"function callCpp1(){ window.external.notify('js String');}\n" +
		"</script></head><body><h3>Test Program</h3><div id='test1'>Message area from C++ Program. Press button.</div>\n" +
		"<input type='button' value='btn1'  onClick='callCpp1()'>" +
		"</body></html>";

	WebView1->NavigateToString(html);
}
void cppxamlWebView1::MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	WebView1->InvokeScript("jsFunc1", nullptr);
}
void cppxamlWebView1::MainPage::wv_notify(Platform::Object^ sender, Windows::UI::Xaml::Controls::NotifyEventArgs e)
{
	TextBox1->Text = e.Value;
}

VisualStudioのデザイナーで作成すると、自動的にたくさんコードを生成するので、重要な部分のみ引用しました。(デフォルトのファイルからこれに関連する部分も修正する必要はあります)使用するXAMLコントロールは、ボタン、テキストボックス、WebViewの3つです。
ボタンを押すと、WebViewのJavaScript関数を呼び出し、divの内容を書き換えます。WebView内のボタンを押すと、JavaScriptがC++の関数を呼び出しテキストボックスを書き換えます。

起動時
webView1

変更後
webView2

偶然、最近のWebViewがらみの話題の延長みたいになってしまいました。

]]>
../../../20130427201/feed/ 0