3

開発を容易にするために、ViewBox を使用してすべてのコンテンツを Window 内にラップしています。これは、開発マシンの画面が展開マシンよりも小さいため、ViewBox を使用するとプロポーションをより適切に実現できるためです。明らかに、コードのリリース バージョンに存在する理由はありません。XAML でその「ラッピング」ViewBox を条件付きで含める/除外する簡単な方法はありますか?

例えば

<Window>
  <Viewbox>
    <UserControl /*Content*/>
  </Viewbox>
</Window>
4

1 に答える 1

3

アクセス可能なリソース ディクショナリに 2 つのコントロール テンプレートを作成します。

彼らはこのように見えるはずです

<ControlTemplate x:key="debug_view">
    <ViewBox>
        <ContentPresenter Content={Binding} />
    </ViewBox>
</ControlTemplate>
<ControlTemplate x:key="release_view">
    <ContentPresenter Content={Binding} />
</ControlTemplate>

次に、これをメインビューで使用できます

<Window>
    <ContentControl Template="{StaticResource debug_view}">
        <UserControl /*Content*/ ...>
    </ContentControl>
</Window>

StaticResource次に、バインディングのルックアップ キーを「debug_view」から「release_view」に変更するだけで、前後に切り替えることができます

より動的にしたい場合は、さらにこれを行うことができます:

<Window>
    <ContentControl Loaded="MainContentLoaded">
        <UserControl /*Content*/ ...>
    </ContentControl>
</Window>

次に、コードビハインドで

void MainContentLoaded(object sender, RoutedEventArgs e)
{
    ContentControl cc = (ContentControl) sender;
#if DEBUG
    sender.Template = (ControlTemplate) Resources["debug_view"];
#else
    sender.Template = (ControlTemplate) Resources["release_view"];
#endif
}

このように、DEBUG シンボルが定義されているかどうかに応じて、異なるテンプレートが選択されます。

于 2010-05-25T23:18:17.587 に答える