私は JavaScript の基本的な経験を持つ完全な初心者ですが、電話アプリを作成するための c#net プログラミングを学びたいと思っています。私がやろうとしているアプリでは、特定の xaml 要素にカスタム プロパティを設定できるようにする必要があります。
stackoverflow でこれの単純な例と思われるものを見つけました ( Adding custom attributes to an element in XAML? ) が機能しませんでした。その後、どこでも多くのドキュメントを読み、これまで以上に混乱を感じます...コンセプトは正しいと思いますが、実装はそうではありません。たとえば、言及したページからコードをコピーすると、次のようになります。
メインページ.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 要素と ac#class をバインドするにはどうすればよいですか?
私が行きたい場所を見つけたら、どんな情報でも大歓迎です。私はすでにこの細部に何時間も費やしました...
よろしくお願いします!!!!!!!!!!!! (はい、私は初心者です...私はいくつかのC#ブックを注文しています。多くのサイトをチェックしましたが、まだガイダンスがありません..ありがとうございました)
編集:フォローアップ:私に与えられた提案は機能し、コンパイルできましたが:
xmlns:local="clr-namespace:MyNamespace" を実行すると、次のエラーが表示されます。
未定義の CLR 名前空間。'clr-namespace' URI は、アセンブリに含まれていない名前空間 'MyNamespace' を参照しています。その行が必要ですか?
そこから、他の要素を追加することはできません。たとえば、その行を削除して楕円を追加すると、MyProperty を指定できません。VS は次のように伝えます。
プロパティ「MyProperty」はタイプ Ellipse で見つかりませんでした。
MyClass に「MyProperty」を登録したことを理解しています。さて、Ellipse に 'MyClass' のクラスを与えるにはどうすればよいでしょうか? 別のアプローチをとるべきですか?「MyProperty」を必要なものに使用できますか? どんなヒントでも大歓迎です、ありがとう!