使用Xamarin.Forms在代码中更改图像

时间:2016-04-11 18:54:56

标签: xamarin.forms

我已经根据官方文档尝试了一些不同的方法,并通过调试我的if / else条件正确触发,但我似乎无法更改我的图像。

正如您所看到的,我在这里尝试了两种方法,但我的理解是因为隐式转换而不需要ImageSource.FromFile

这是XAML:

<TableView Intent="Settings">
        <TableRoot>
            <TableSection Title="Getting Started">
                <ViewCell>
                    <StackLayout Orientation="Horizontal" Padding="10" >
                        <Image Source="bulb.png" x:Name="imgBulb" />
                        <Switch x:Name="bulbSwitch" IsToggled="false" Toggled="bulbSwitchToggled" HorizontalOptions="EndAndExpand" VerticalOptions="Center" />
                    </StackLayout>
                </ViewCell>
            </TableSection>
            <TableSection Title="Time to slide....">
                <ViewCell>
                    <StackLayout Orientation="Vertical" Padding="10" >
                        <Slider Maximum="100" Minimum="0" Value="50" ValueChanged="sliderChanged" ></Slider>
                        <Label x:Name="lblSliderOutput" HorizontalOptions="CenterAndExpand" Text="Placeholder" />
                    </StackLayout>
                </ViewCell>
            </TableSection>
        </TableRoot>
    </TableView>

背后的代码:

public partial class TableViewsPage : ContentPage
{
    public TableViewsPage ()
    {
        InitializeComponent ();
        lblSliderOutput.Text = "50";
    }

    public void sliderChanged(object sender, ValueChangedEventArgs args) {
        var simpleValue = Convert.ToInt32 (args.NewValue);
        lblSliderOutput.Text = simpleValue.ToString();
    }

    public void bulbSwitchToggled(object sender, ToggledEventArgs args) {
        if (imgBulb.Source == ImageSource.FromFile("bulb.png"))
        {
            imgBulb.Source = ImageSource.FromFile ("biggerBulb.png");
            Debug.WriteLine ("imgbulb = bulb.png");
        } 
        else 
        {
            imgBulb.Source = "bulb.png";
            Debug.WriteLine ("imgbulb = biggerBulb.png");
        }
    }
}

0 个答案:

没有答案