2013-02-11 2 views
1

저는 자바 스크립트에서 기본적인 경험을 갖고 있지만 전화 앱을 만들기 위해 C# net 프로그래밍을 배우고 싶습니다. 내가하려고하는 응용 프로그램은 특정 xaml 요소에 대한 사용자 지정 속성을 설정할 수 있어야합니다.Windows 용 실버 라이트에 첨부 된 속성을 사용할 수 없습니다.

나는 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); 
     } 
    } 
} 
} 

, 내가 할 수있는 : 난 그냥 했나요 페이지에서 thecode를 복사 할 경우 exemple를 들어,이 수 이 오류가 있기 때문에 시각적 요소를 추가하지 마십시오.

오류 1 'FrameworkPropertyMetadata'형식 또는 네임 스페이스 이름을 찾을 수 없습니다 (사용 지시문 또는 어셈블리 참조가 누락 되었습니까?)

자,이 코드가 제대로 작동한다면 첨부 된 속성은 내 클래스의 objetc에서만 사용할 수 있습니다 ... xaml 요소와 C# 클래스를 어떻게 바인딩합니까?

내가 어디로 가고 싶은지 알게되면 어떤 정보라도 알려 주시면 감사하겠습니다. 나는이 작은 세부 사항에 이미 시간을 보냈다 ...

미리 감사드립니다 !!!!!!!!!!!!! 후속 : 는

편집을 (예, 멍청한 놈 ... 나는 다시 많은 사이트 여전히 nedd guidance..thanks을 확인, 일부 C#을 책이 오는 것을 주문한있어) 제안을

에 xmlns : = 로컬 "CLR-네임 스페이스를 : MyNamespace에"를했다 나 컴파일 할 수 있지만, 나에게 주어진 :

정의되지 않은 CLR 네임 스페이스 나에게 오류를 제공합니다. 'clr-namespace'URI는 어셈블리에 포함되지 않은 'MyNamespace'네임 스페이스를 참조합니다. 그 라인이 필요한가요?

거기에서 다른 요소를 추가 할 수 없습니다. 해당 줄을 제거하고 타원을 추가하면 MyProperty를 부여 할 수 없습니다. VS는 다음과 같이 알려줍니다.

타원형에서 Property'MyProperty '을 (를) 찾을 수 없습니다.

MyClass에 'MyProperty'를 등록한 것으로 알고 있습니다. 음, 타원에 'MyClass'클래스를 어떻게 부여합니까? 다른 접근 방법을 취해야합니까? 내가 원하는대로 'MyProperty'를 사용할 수 있습니까? 모든 팁을 높이 평가, 감사합니다!

답변

0

모든 것이 대부분 괜찮아 보입니다. FrameworkPropertyMetadata에서 PropertyMetadata으로 변경하면됩니다. 그러면 코드가 올바르게 컴파일되어야합니다.

업데이트 : XAML 네임 스페이스는 실제로 잘못되었습니다. PhoneApp1 네임 스페이스 내에 MyNamespace를 정의했습니다. 따라서 전체 네임 스페이스는 PhoneApp1입니다.네임 스페이스가 분류 한 번 연결된 속성은 간단하다 적용

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

This article has more on xaml namespaces in Silverlight

하는 myNameSpace.

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

This article about Attached Properties should help

+0

안녕, 실제로 단지 PropertyMetadata 날 실행할 수 있도록 작성 자넬 감사 : 사실, 당신은 이미

shell:SystemTray.IsVisible="True"

당신의 연결 속성과 같이 적용됩니다 연결된 속성을 사용했다 전화 시뮬레이터. 이상한 FrameWorkPropertyMetadata 내가 언급 한 게시물에서 작동하는 것 같았다. –

+0

감사합니다 !!!!! 내가 너에게 맥주 한두 대 빚진 것 같아. 나는 그걸로 너무 많은 시간을 보냈다. 링크도 주셔서 감사합니다. 정말 감사드립니다. 다시 한 번 감사드립니다. 작업 코드를 사용하면 필요한 신뢰를 얻을 수 있습니다. –

+0

나는이 게시물을 읽어야 할 나 같은 놈을 추가해야한다. 네임 스페이스를 입력 할 때 선언은 코드의 왼쪽 끝까지 가야한다. 그 사이의 어떤 공간도 보이지 않게합니다 ... –

관련 문제