I’m a total noob with basic experience in javascript but really would like to learn c#net programming to make phone apps. The app I’m trying to do needs me to be able to set a custom property on certain xaml elements.
I found what seems to be a simple exemple of this on stackoverflow (Adding custom attributes to an element in XAML?) and didn’t get it work. I then read a lot of documentation everywhere and feel more confused than ever…I think I got the concept right, but my implementation is not. For exemple, if I just copy thecode from the page I mentionned, I get this:
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);
}
}
}
}
From there, I can’t add any visual element because I have this error:
Error 1 The type or namespace name ‘FrameworkPropertyMetadata’ could not be found (are you missing a using directive or an assembly reference?)
Now, if this code worked, from what I understand, the attached property would only be avalaible to objetc of my class… How do you bind a xaml element and a c#class?
If you see where i’d like to go, any informatio you have will be appreciated. I already spent hours on this little detail…
Many thanks in advance!!!!!!!!!!!!!
(Yes, I’m a noob…I have some c#books ordered that are coming, I checked many sites but still nedd guidance..thanks again)
EDIT:
Follow up: The suggestion given to me worked and let me compile but:
The xmlns:local=”clr-namespace:MyNamespace” gives me this error:
Undefined CLR namespace. The ‘clr-namespace’ URI refers to a namespace ‘MyNamespace’ that is not included in the assembly. Do I need that line?
From there I can’t add any other element. If I do remove that line, and add an ellipse, for exemple, I can’t give it a MyProperty, VS tells me that :
The Property’MyProperty’ was not found in type Ellipse.
I understand I registered ‘MyProperty’ for MyClass. Well, How do I give an Ellipse a class of ‘MyClass’? Should I take another approach? Can I use ‘MyProperty’ on anything I want? Any tips highly appreciated, THANKS!
Everything looks fine for the most part.
You just need to change
FrameworkPropertyMetadatatoPropertyMetadata, and the code should compile just fine.Update: The XAML namespace is actually wrong. You have defined MyNamespace inside the PhoneApp1 namespace. Therefore the complete namespace is PhoneApp1.MyNamespace
This article has more on xaml namespaces in Silverlight
Applying your attached property is simple enough once you have the namespace sorted. In fact, you have used an attached property already
Your attached property will be applied like so:
This article about Attached Properties should help