質問
2018年11月14日水曜日 7:47
この問題で、ここ2年来ずっと頭を悩ませているものです。
完全に解決できるまで御指導をお願いします。
’XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ctl="clr-namespace:_1114a"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
>
<Window.Resources>
<ctl:Class1 Name="AAA"/>
</Window.Resources>
</Window>
'VB.netソース
Imports System.ComponentModel
Class MainWindow
End Class
Public Class Class1 ’内容は最低限にしてある
Implements INotifyPropertyChanged
Public Property Name As String
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
End Sub
End Class
これだけの簡単なものですが、
XAMLの xmlns:ctl="clr-namespace:_1114a" ← この部分で
エラー XDG0008 名前 "Class1" は名前空間 "clr-namespace:_1114a" に存在しません。
エラー XDG0046 すべてのディクショナリ エントリにキーが関連付けられている必要があります。
エラー XLS0515 IDictionary に追加されるすべてのオブジェクトは、Key 属性またはオブジェクトに関連する別の型のキーを保持している必要があります。
このエラーが消えません。WEBサイトに掲載されているどんな解決法も通用しません。
プロジェクトのプロパティー→アプリケーションルート→名前空間には「_1114a」と設定されているのでxmlns:ctl="clr-namespace:_1114a"は間違っていないと思います。
開発環境はWindows7;VS2017 Community15.7.3です。
御教授いただきたい事は
(1) どこをどうすればエラーを消すことができますか。
(2) 今までこの種のプログラムを書いてきているのですが、このエラーが発生一度発生すると、プロジェクトを何度も最初から作成し直したり、何か偶発的な事象が起こらないとエラーが消えないのです。動作が確認されているプログラムを書き換えても、どこかで挫折します。
やりたい事は、http://gushwell.ldblog.jp/archives/52300866.html このサイトをVB.netで動作させたいのですが、C#が読めないのでVB.netへの翻訳が必要なのです。以上です。
よろしくお願い致します。
すべての返信 (5)
2018年11月14日水曜日 8:43 ✅回答済み | 1 票
リソースなのにKey属性が無いからだと思います。以下のようにKey属性を追加してみて下さい。
<ctl:Class1 x:Key="aaa" Name="AAA"/>
★良い回答には質問者は回答済みマークを、閲覧者は投票を!
2018年11月14日水曜日 11:06 ✅回答済み | 1 票
失;しました。
INotifyPropertyChanged インターフェースを実装している場合は、以下です。(イベントのメンバー定義の後ろに、追加している)
Public Class Class1
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(info As String)
' VB.NET の場合、Nothing チェックしなくてもいい仕様
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
End Class
2018年11月14日水曜日 10:29
<ctl:Class1 x:Key="aaa" Name="AAA"/>
↑本当だ!これでエラーが消えました。どうもありがとうございました。
’’’以下のXAMLでコンパイルが通りましたので、備忘録として記載しておきます。(ここのフォーラムで質問させていただいた事と皆様の回答は機会がある度に何度も見直しているからです。)
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ctl="clr-namespace:_1114a"
Title="MainWindow" Height="450" Width="800"
>
<Window.Resources>
<ctl:Class1 x:Key="aaa" Name="AAA"/>
</Window.Resources>
</Window>
しかし、質問で記載した http://gushwell.ldblog.jp/archives/52300866.html このサイトの中にC言語のコードがあるのですが、
private void NotifyPropertyChanged(String info) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
これが読めません。この部分だけで良いですからVB.netに翻訳していただきたいです。どなたかよろしくお願い致します。
2018年11月14日水曜日 11:00
以下です。
Imports System.ComponentModel
~
Public Event PropertyChanged As PropertyChangedEventHandler
Private Sub NotifyPropertyChanged(info As String)
' VB.NET の場合、Nothing チェックしなくてもいい仕様
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
2018年11月16日金曜日 3:00
sutefu7さんの御回答を元に解決できました。
完全解決ができたことを報告します。
'XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ctl="clr-namespace:_1114a"
DataContext="{DynamicResource Custom01}"
Title="MainWindow" Height="450" Width="800"
>
<Window.Resources>
<ctl:Class1 x:Key="Custom01" Name="Click CommandButton" />
</Window.Resources>
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Height="103" Margin="125,126,0,0" VerticalAlignment="Top" Width="271" Click="Button_Click"/>
<TextBox Name="textBox1" Text="{Binding Name, Mode=TwoWay}" Margin="93,271,109,100" />
</Grid>
</Window>
’VB
Imports System.ComponentModel
Class MainWindow
'Me.DataContextはXAMLで定義、参照されている
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Me.DataContext.Name = "ここに表示されているのがClass1形式のMe.DataContextから読み取られたプロパティーである。textBox1.Textと双方向バインドされている"
End Sub
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
End Sub
Private Sub MainWindow_Closed(sender As Object, e As EventArgs) Handles Me.Closed
'textBox1.textの内容が表示される
MessageBox.Show(Me.DataContext.Name)
End Sub
End Class
Public Class Class1
Implements INotifyPropertyChanged
Private Property _name As String
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
If value <> _name Then
_name = value
NotifyPropertyChanged("Name")
End If
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(info As String)
' VB.NET の場合、Nothingをチェックしなくても良い仕様
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
End Class
この方法で、プログラム全体で参照される設定情報とTextBox.textとの双方向バインディングができるようになりました。これまでに作ったプログラムをこの方法で書き直したいと思います。お二方には御回答いただき大変参考になりました。どうもありがとうございました。