トップ回答者
VB.net VisualTreeHelperの拡張メソッドについて

質問
-
毎々、お世話になっております。
WPFでVisualTreeHelperの機能が不足していたため、拡張メソッドを追加して使用したいのですが
インテリセンスには表示されるのですが、いざ使おうとするとエラーになってしまいます。
「非共有メンバを参照するには、オブジェクト参照が必要です。」VisualTreeHelperの定義を見たところ
NotInheritableキーワードがついていてメンバのメソッドがSharedになっているのですが
この場合、拡張メソッドを追加することができないのでしょうか?
初歩的な質問かもしれませんが、ご教授いただけますでしょうか?Imports System.Windows.Interactivity ''' <summary> ''' サンプルAction ''' </summary> Public Class SampleAction : Inherits TriggerAction(Of DependencyObject) Protected Overrides Sub Invoke(parameter As Object) Dim x = VisualTreeHelper.GetParentEx(AssociatedObject, GetType(Window)) End Sub End Class
Imports System.Runtime.CompilerServices ''' <summary> ''' VisualTreeHelperの拡張のメソッド ''' </summary> Module VisualTreeHelperExtensions ''' <summary> ''' ビジュアルオブジェクトの指定された型の親を探す ''' </summary> ''' <param name="this">拡張対象の型</param> ''' <param name="reference">親が返されるビジュアル</param> ''' <param name="targetType">対象型</param> ''' <returns></returns> <Extension> Public Function GetParentEx(ByVal this As VisualTreeHelper, ByVal reference As DependencyObject, ByVal targetType As Type) As DependencyObject Dim parent = VisualTreeHelper.GetParent(reference) If IsNothing(parent) OrElse parent.GetType.BaseType Is targetType Then Return parent Else Return GetParentEx(this, reference, targetType) End If End Function End Module
----------------------------------------
[環境]
VisualStudio 2015 Community
WPF(VB.net)
.Net Framework4.6.00081
Windows7 Pro x64 SP1
回答
すべての返信
-
Tak1wa 様
早々のご回答ありがとうございました。
VisualTreeHelperについて拡張メソッドは利用できない旨、理解できました。他の方も確認されていたようなので参考ページのリンクを貼ります。
https://social.msdn.microsoft.com/Forums/vstudio/ja-JP/30cb5f05-6f6c-4833-bc78-afde5217275d?forum=csharpgeneralja