【问题标题】:Still can't use attached properties in silverlight for windows phone仍然无法在 silverlight for windows phone 中使用附加属性
【发布时间】:2013-01-27 02:34:43
【问题描述】:

我是一个具有 javascript 基本经验的菜鸟,但我真的很想学习 c#net 编程来制作手机应用程序。我正在尝试做的应用程序需要我能够在某些 xaml 元素上设置自定义属性。

我在 stackoverflow (Adding custom attributes to an element in XAML?) 上发现了一个看似简单的例子,但没有得到它的工作。然后,我到处阅读了大量文档,感到比以往任何时候都更加困惑......我认为我的概念是正确的,但我的实现不是。例如,如果我只是从我提到的页面中复制代码,我会得到:

Mainpage.xaml

<phone:PhoneApplicationPage 
x:Class="PhoneApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:local="clr-namespace:MyNamespace" //<--ADDED BY ME
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"

shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->


    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
</Grid>
</phone:PhoneApplicationPage>

Mainpage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }
 }
namespace MyNamespace
{
    public static class MyClass
    {

        public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached("MyProperty",
            typeof(string), typeof(MyClass), new FrameworkPropertyMetadata(null));

        public static string GetMyProperty(UIElement element)
        {
            if (element == null)
                throw new ArgumentNullException("element");
            return (string)element.GetValue(MyPropertyProperty);
        }
        public static void SetMyProperty(UIElement element, string value)
        {
            if (element == null)
                throw new ArgumentNullException("element");
            element.SetValue(MyPropertyProperty, value);
        }
    }
}
}

从那里,我无法添加任何视觉元素,因为我有这个错误:

错误 1 ​​找不到类型或命名空间名称“FrameworkPropertyMetadata”(您是否缺少 using 指令或程序集引用?)

现在,如果此代码有效,据我了解,附加属性将仅对我的类的 objetc 可用...您如何绑定 xaml 元素和 c#class?

如果您看到我想去的地方,我们将不胜感激您提供的任何信息。我已经在这个小细节上花了几个小时......

非常感谢!!!!!!!!!!!!! (是的,我是菜鸟...我订购了一些即将推出的 c#books,我检查了许多网站,但仍然需要指导...再次感谢)

编辑: 跟进:给我的建议有效,让我编译但是:

xmlns:local="clr-namespace:MyNamespace" 给了我这个错误:

未定义的 CLR 命名空间。 “clr-namespace” URI 指的是程序集中未包含的命名空间“MyNamespace”。我需要那条线吗?

从那里我不能添加任何其他元素。如果我确实删除了那条线,并添加了一个椭圆,例如,我不能给它一个 MyProperty,VS 告诉我:

在 Ellipse 类型中找不到属性“MyProperty”。

我了解我为 MyClass 注册了“MyProperty”。那么,我如何给 Ellipse 一个“MyClass”类?我应该采取另一种方法吗?我可以在任何我想要的东西上使用“MyProperty”吗?任何提示高度赞赏,谢谢!

【问题讨论】:

    标签: c# silverlight properties


    【解决方案1】:

    大部分情况下一切看起来都很好。 只需将FrameworkPropertyMetadata 更改为PropertyMetadata,代码就可以正常编译了。

    更新: XAML 命名空间实际上是错误的。您已经在 PhoneApp1 命名空间内定义了 MyNamespace。因此完整的命名空间是PhoneApp1.MyNamespace

    xmlns:local="clr-namespace:PhoneApp1.MyNamespace"
    

    This article has more on xaml namespaces in Silverlight

    对命名空间进行排序后,应用附加属性就足够简单了。实际上,您已经使用了附加属性

    shell:SystemTray.IsVisible="True"

    您的附加属性将像这样应用:

    <Grid local:MyClass.MyProperty="Value" />
    

    This article about Attached Properties should help

    【讨论】:

    • 嗨,谢谢。是的,确实只是编写 PropertyMetadata 让我运行手机模拟器。奇怪,因为 FrameWorkPropertyMetadata 似乎在我提到的帖子中有效。
    • 非常感谢您的宝贵时间!!!!!!好像我欠你一两杯啤酒......我不敢相信它现在有效,因为我花了这么多时间!也感谢您的链接。我真的很感激,再次感谢......拥有一个工作代码会给我我需要的信心。
    • 我必须为像我这样需要阅读这篇文章的新手添加:当您键入命名空间时,声明必须位于代码的最左侧。之间的任何空间都使它不可见...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 2011-12-23
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多