如何在视图模型方法中将绑定属性用作参数

时间:2019-06-24 07:57:44

标签: c# xamarin data-binding xamarin.forms prism

我有一个使用Prism框架的Xamarin Forms视图模型。我的视图模型中有很多UI元素,每个元素都有很多具有相似行为的绑定属性。在执行命令操作之后,我想为每个元素执行完全相同的逻辑,而不必为每个元素重复相同的逻辑。

更具体。我在图片上有地标。每个地标均可拖动。每个地标都有一个激活拖动模式的命令,该命令会更改地标的图标及其大小。当我创建一个方法以一种方法将该行为模块化时,将每个界标绑定属性作为参数接收,则永远不会触发属性的设置器,因此也不会更改UI元素。

这是我尝试过的:

我的地标性XAML

<Controls:LandmarkView 
                                    DragMode="Touch" 
                                    LBounds="{Binding ChinLandmarkBounds, Mode=TwoWay}" 
                                    DragDirection="All"
                                    IsVisible="{ Binding AreLandmarksVisible }"
                                    ToggleDraggingCommand="{Binding ChinToggleDraggingCommand}"
                                    DraggingBoundaries="{Binding ChinDraggingBoundaries}">
                                    <Controls:LandmarkView.Content>
                                        <AbsoluteLayout>
                                            <Image BackgroundColor="#72C8BB" 
                                                   WidthRequest="{Binding LandmarkLineWidth}" 
                                                   AbsoluteLayout.LayoutBounds= "{Binding LandmarkLineXPos}"
                                                   AbsoluteLayout.LayoutFlags="YProportional,WidthProportional"/>
                                            <ffimageloading:CachedImage 
                                                WidthRequest="{Binding ChinSize}"
                                                HeightRequest="{Binding ChinSize}"
                                                Source="{Binding ChinLandmarkIcon, Converter={StaticResource SvgImageSourceConverter}}">
                                            </ffimageloading:CachedImage>
                                        </AbsoluteLayout>
                                    </Controls:LandmarkView.Content>
                                </Controls:LandmarkView>

视图模型中用于拖动切换的命令:

public ICommand ChinToggleDraggingCommand { get; set; }

界标绑定属性:

private Rectangle _chinLandmarkBounds;
    public Rectangle ChinLandmarkBounds
    {
        get { return _chinLandmarkBounds; }
        set
        {
            SetProperty(ref _chinLandmarkBounds, value);
            CalculateMouthRightEdgeDraggingBoundaries();
            CalculateMouthLeftEdgeDraggingBoundaries();
            CalculateChinDraggingBoundaries();
        }
    }
    private string _chinLandmarkIcon;
    public string ChinLandmarkIcon
    {
        get { return _chinLandmarkIcon; }
        set { SetProperty(ref _chinLandmarkIcon, value); }
    }
    private Rectangle _chinDraggingBoundaries;
    public Rectangle ChinDraggingBoundaries
    {
        get { return _chinDraggingBoundaries; }
        set { SetProperty(ref _chinDraggingBoundaries, value); }
    }
    private int _chinSize;
    public int ChinSize
    {
        get { return _chinSize; }
        set { SetProperty(ref _chinSize, value); }
    }

我想为每个不会触发属性设置器的界标调用的方法:

private void ToggleDraggingMode(Rectangle landmarkBounds, string landmarkIcon, int size, bool activateDraggingMode)
    {
        if (activateDraggingMode)
        {
            landmarkIcon = ControlsArrowsAllRed;
            size = size * 2;
            landmarkBounds = new Rectangle((double)landmarkBounds.X - size / 2, (double)landmarkBounds.Y - size / 2, size, size);
        }
        else
        {
            landmarkIcon = ControlsRedFull;
            size = size / 2;
            landmarkBounds = new Rectangle(landmarkBounds.X + size / 2, (double)landmarkBounds.Y + size / 2, size, size);
        }
    }

对先前方法的调用:

ToggleDraggingMode(ChinLandmarkBounds, ChinLandmarkIcon, ChinSize, activateDraggingMode);

当我给方法参数赋值时(在上一代码中,landmarkBounds局部变量),如何将绑定属性作为方法的参数传递,并让设置器被调用?

谢谢

0 个答案:

没有答案