对于具有通用控件的多个 Silverlight 应用程序来说,什么是好方法?

发布于 2024-12-12 18:12:15 字数 558 浏览 0 评论 0原文

我对 Silverlight 还很陌生,必须编写几个 SL 应用程序,这些应用程序将共享许多东西。

我计划将所有常见的 C# 代码放在一个单独的程序集中,但我对 XAML 部分真的很恼火。

我的所有应用程序都会有一些通用控件:

  • 顶部的菜单栏、
  • 顶部的工具栏、
  • 底部的状态栏、
  • 左侧的面板。

我在网上搜索提示,发现XAML代码不能被继承。

因此,我考虑创建一个包含常用控件的大型自定义控件,然后将其添加到所有应用程序的主页上,但这似乎不是正确的方法。

这里最好的做法是什么?

感谢任何帮助,

编辑:我想要在这里得到的是我已经用 WinForms 做过的事情。我创建了一个“基础”WinForm 并在其上放置了一些控件。然后,当我创建继承这个“基本”WinForm 的新 WinForm 时,它们拥有所有基本控件。我不必使用任何自定义控件。

但我知道 Silverlight 不是 WinForm,我想要的可能以这种方式不可行(或根本不可行?)。

I'm pretty new to Silverlight and have to code several SL applications which will share many things.

I plan to put all common C# code in a separated assembly but I'm really annoyed about the XAML part.

All my applications will have some common controls:

  • a menu bar at the top,
  • a toolbar at the top,
  • a status bar at the bottom,
  • a panel on the left.

I searched the web for hints and discovers that XAML code cannot be inherited.

So I thought about creating a big custom control containing my common controls and then adding it on the main page of all my applications but that doesn't seem to be the right way.

What's the best practice, here?

Any help appreciated,

EDIT: What I'd like to get here, is something I already did with WinForms. I created a "base" WinForm and put some controls on it. Then, when I created new WinForms which inherited this "base" WinForm, they had all the basic controls. I didn't have to use any custom control.

But I understand that Silverlight is not WinForm and what I want might not be feasible this way (or at all?).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-12-19 18:12:15

这取决于您想要采取的路线,无论您是想使用一些模板等创建一个资源文件,将其单独复制/链接到每个应用程序中,还是使用公共 DLL 中包含的内容。

如果您想做后者,这里有一种方法。子类 ContentControl 在其控件模板中定义通用布局(您可以在 XAML 中完成这一切),包括在适当位置对其内容进行绑定:

<ContentControl x:Class="MyLibrary.MyLayoutControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ContentControl.Template>
        <ControlTemplate TargetType="ContentControl">
            <!-- this could be a layout grid with various other controls in it -->
            <Border BorderBrush="Red" BorderThickness="2" CornerRadius="5">
                <ContentPresenter Content="{TemplateBinding Content}" />
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

然后在应用程序根控件中使用它:

<UserControl x:Class="InCustomControlTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:MyLibrary;assembly=MyLibrary">

    <c:MyLayoutControl>
        <Button Content="Hi I am a button" />
    </c:MyLayoutControl>
</UserControl>

如果您想要做更复杂的事情,你应该制作一个自定义控件;尽管在大多数情况下,您可能可以将一些用于各种组件(例如状态栏)的用户控件放在正确的位置。

It depends on the route you want to take, whether you want to make one resources file with some templates etc, which is copied/linked into each application seperately, or use something included in a common DLL.

If you want to do the latter, here's one approach. Subclass ContentControl to define your common layout in its control template (you can do this all in XAML), including a binding to its Content in the appropriate place:

<ContentControl x:Class="MyLibrary.MyLayoutControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ContentControl.Template>
        <ControlTemplate TargetType="ContentControl">
            <!-- this could be a layout grid with various other controls in it -->
            <Border BorderBrush="Red" BorderThickness="2" CornerRadius="5">
                <ContentPresenter Content="{TemplateBinding Content}" />
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

Then use it in your application root control:

<UserControl x:Class="InCustomControlTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:MyLibrary;assembly=MyLibrary">

    <c:MyLayoutControl>
        <Button Content="Hi I am a button" />
    </c:MyLayoutControl>
</UserControl>

If you want to do something more sophisticated, you should make a custom control; though in most cases you may instead be able to get away with a few UserControls for various components (eg your status bar) that you drop into the right place.

江南烟雨〆相思醉 2024-12-19 18:12:15

将xaml资源放入库项目中没有什么大问题。
从其他项目引用资源字典时,请务必包含程序集名称。

模板上还有继承,您可以在其中放置 BasedOn 属性。

编辑:好吧,看看我们前一段时间处理的项目,我现在记得我们实际上遇到了问题,并将 xaml 资源复制到每个项目......我忘了=)
我认为它有效,但也带来了一些问题。也许将 xaml 文件添加为链接?

就我个人而言,我也不会创建很多控件,而是为大多数内容创建模板。如果您有控件,最好仅使用模板使用 C# 编写它们,从而防止带有代码隐藏的 xaml。只是我个人的经验,可能不是通用的指导方针。

There is no big problem putting xaml resources in library projects.
When referencing resourcedictionaries from other projects, be sure to include the assembly name.

Also there is inheritance on templates, where you can put BasedOn attribute.

Edit: well, looking at the project we worked on some time ago, I now remember we actually had problems with it and had the xaml resources copied to each project... I forgot =)
I think it worked but brought some problems here and there. Maybe add the xaml files as links?

Personally I would also not create to many controls, rather have templates for most stuff. If you have controls, better write them in c# only with usage of templates, preventing xaml with code behinds. Just my personal experience, might not be the common guideline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文