C# 如何从另一个类访问控件的名称?

C# 如何从另一个类访问控件的名称?,c#,xamarin.forms,C#,Xamarin.forms,在我的项目中,我有一个主页面,其中有一个控件,还有一个ViewModel,我在其中控制该控件对绑定的操作。我需要做的是能够从ViewModel访问该控件的x:Name,但我不知道如何做,因为我无法为其分配绑定。有解决办法吗 我的代码: MainPage.xaml: 您可以将Floafing按钮作为参数传递给命令 在Floafing按钮中。xaml 在ContentPage中 因为您使用了MVVM,所以最好避免在ViewModel中访问元素和视图(并且我们只能在xaml中设置x:name)。你想

在我的项目中,我有一个主页面,其中有一个控件,还有一个ViewModel,我在其中控制该控件对绑定的操作。我需要做的是能够从ViewModel访问该控件的x:Name,但我不知道如何做,因为我无法为其分配绑定。有解决办法吗

我的代码:

MainPage.xaml:


您可以将Floafing按钮作为参数传递给命令

在Floafing按钮中。xaml 在ContentPage中
因为您使用了MVVM,所以最好避免在ViewModel中访问元素和视图(并且我们只能在xaml中设置x:name)。你想达到什么效果?我想我们不需要访问控件就可以找到更好的解决方案。我的控件是浮动按钮。我想做的是能够从ViewModel访问此控件,这样当我单击它时,我就可以在按钮上制作动画。你能提供浮动按钮的代码(xaml和代码隐藏)吗?检查我的更新在MVVMI中检查此动画我不能将此代码用于我的浮动按钮。xaml。我在ImageButton中已经有了命令。我用当前的FloatingButton代码编辑了我的帖子。你想单击哪个ImageButton并播放动画?我的想法是,当我单击主按钮时,它将制作一个小动画,然后其他小按钮在出现和消失时,将以淡入淡出的方式出现。由于能够为我提供至少一个动画,因此您可以检查我的代码,设置ImageButton的命令和命令参数。我的代码中的xxxCommand仅用于演示,您可以将其定义为类似openFloatingButton的任何命令。
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            //...
             x:Name="FloatingButtonView">
<ImageButton
       x:Name="listita"
       Padding="10"
      
       Command="{Binding Source={x:Reference FloatingButtonView}, Path=LaunchWeb}"
       CommandParameter="{Binding Source={x:Reference FloatingButtonView}, Path=Website}"
       //...  
/>
 public static readonly BindableProperty LaunchWebProperty =
                BindableProperty.Create(nameof(LaunchWeb), typeof(ICommand), typeof(FloatingButton));

    public ICommand LaunchWeb
    {
        get => (ICommand)GetValue(LaunchWebProperty);
        set => SetValue(LaunchWebProperty, value);
    }



    public static BindableProperty WebsiteProperty =
        BindableProperty.Create(nameof(Website), typeof(object), typeof(FloatingButton));

    public object Website
    {
        get => (object)GetValue(WebsiteProperty);
        set => SetValue(WebsiteProperty, value);
    }
<fav:FloatingButton
                x:Name="FloatButton"
                CollectionViewVisible="{Binding IsVisible}"
                ItemSource="{Binding ItemList}"
                LaunchWeb="{Binding xxxCommand}"
                Website="{Binding Source={x:Reference FloatButton},Path=.}"
                PrimaryButtonColor="{Binding FirstButtonColor}"
                PrimaryImageSource="{Binding FirstImage}" />
 public ICommand xxxCommand { get; set; }
xxxCommand = new Command((obj)=> {

   var FloatButton = obj as FloatingButton  ;
   //... do something you want  
            
 });