I recently started to learn Xamarin and am now trying to find a solution for my project. I have a code in HAML and I can fill my order with dishes with ingredients. Dishes with ingredients are an expander, and I want to add a grouping by guests. Guests can be 1, 2 or more, and each has its own set of dishes. I tried to use CollectionView to group, but there is an error on UWP --- GitHub # 10890.
Maybe I should create my order in code, but I don’t understand how to make bindings to the Expander (((
I need one more grouping: “guest 1”, “guest 2”, etc. In one order there are several guests and each has his own list, and I also need to add a dish to the list of a particular guest when adding.
I need a structure (image):
Guest 1
Dish 1
Ingredient 1
Ingredient 2
Guest 2
Dish 1
Ingredient 1
Ingredient 2
Ingredient 3
Dish 2 (no ingredients)
Dish 3
Ingredient 1
Ingredient 2
My XAML without groups:
<ScrollView Grid.Row="1" Grid.RowSpan="5" Grid.Column="1" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<StackLayout BindableLayout.ItemsSource="{Binding Path=Items}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<BoxView BackgroundColor="{Binding Color}" WidthRequest="3" HorizontalOptions="Start"
VerticalOptions="FillAndExpand"/>
<syncfusion:SfExpander Grid.Column="1" >
<syncfusion:SfExpander.Header>
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3.5*"/>
</Grid.ColumnDefinitions>
<BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start"
VerticalOptions="FillAndExpand"/>
<StackLayout Grid.Column="2" HorizontalOptions="Start" VerticalOptions="Center" Margin="20">
<Label Text="{Binding Name}" TextColor="Black" FontSize="15"/>
</StackLayout>
</Grid>
</syncfusion:SfExpander.Header>
<syncfusion:SfExpander.Content>
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3.5*"/>
</Grid.ColumnDefinitions>
<BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start"
VerticalOptions="FillAndExpand"/>
<StackLayout Grid.Column="2" Spacing="10">
<Label Text="Ingredients" TextColor="Black" Opacity="0.5" FontSize="10" Margin="20,0"/>
<StackLayout BindableLayout.ItemsSource="{Binding Items}" HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label TextColor="#2F3246" FontSize="12">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="{Binding Name}" FontAttributes="Bold"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</Grid></syncfusion:SfExpander.Content>
</syncfusion:SfExpander>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ScrollView>
<!--Order Test-->
I tried to do this in code, it works and gives me the desired grouping, but I don’t understand how to implement PropertyChanged
XAML:
<Grid x:Name="OrderGrid" Grid.Row="1" Grid.RowSpan="5" Grid.Column="1" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
</Grid>
CODE:
scrollView = new ScrollView();
stack = new StackLayout();
var List = new OrderPageViewModel();
OrderList = List.Items;
if (OrderList != null)
foreach (var shoppingItem in OrderList)
{
expander = new Expander();
//Expander Header
var headergrid = new Grid()
{
HeightRequest = 60
};
var headerLabel = new Label()
{
TextColor = Color.White,
BackgroundColor = Color.Teal,
HorizontalTextAlignment = TextAlignment.Center,
Text = shoppingItem.Name,
VerticalTextAlignment = TextAlignment.Center
};
headergrid.Children.Add(headerLabel);
expander.Header = headergrid;
var contentgrid = new Grid();
var stackExpanderContent = new StackLayout();
foreach (var shoppingDetailItem in shoppingItem.Items)
{
//Expander Content
var contentLabel = new Label()
{
TextColor = Color.Black,
BackgroundColor = Color.White,
Text = shoppingDetailItem.Name,
VerticalTextAlignment = TextAlignment.Center,
HeightRequest = 50
};
stackExpanderContent.Children.Add(contentLabel);
}
contentgrid.Children.Add(stackExpanderContent);
expander.Content = contentgrid;
stack.Children.Add(expander);
}
scrollView.Content = stack;
scrollView.VerticalOptions = LayoutOptions.StartAndExpand;
OrderGrid.Children.Add(scrollView);
You need to reconstruct your xaml and model class .
It is a bit complex than the original one .
Xaml
<ScrollView Grid.Row="1" Grid.RowSpan="5" Grid.Column="1" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<StackLayout BindableLayout.ItemsSource="{Binding Path=Items}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding ID}" TextColor="Red" FontSize="12"/>
<StackLayout BindableLayout.ItemsSource="{Binding Path=Items}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<BoxView BackgroundColor="Red" WidthRequest="3" HorizontalOptions="Start" VerticalOptions="FillAndExpand"/>
<syncfusion:SfExpander Grid.Column="1" >
<syncfusion:SfExpander.Header>
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3.5*"/>
</Grid.ColumnDefinitions>
<BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start"
VerticalOptions="FillAndExpand"/>
<StackLayout Grid.Column="2" HorizontalOptions="Start" VerticalOptions="Center" Margin="20">
<Label Text="{Binding Name}" TextColor="Black" FontSize="15"/>
</StackLayout>
</Grid>
</syncfusion:SfExpander.Header>
<syncfusion:SfExpander.Content>
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3.5*"/>
</Grid.ColumnDefinitions>
<BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start"
VerticalOptions="FillAndExpand"/>
<StackLayout Grid.Column="2" Spacing="10">
<Label Text="Ingredients" TextColor="Black" Opacity="0.5" FontSize="10" Margin="20,0"/>
<StackLayout BindableLayout.ItemsSource="{Binding Items}" HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label TextColor="#2F3246" FontSize="12">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="{Binding Name}" FontAttributes="Bold"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</Grid>
</syncfusion:SfExpander.Content>
</syncfusion:SfExpander>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ScrollView>
Code hebind
public class Item
{
public string Name { get; set; }
}
public class Model
{
public string Name { get; set; }
public List<Item> Items { get; set; }
}
public class Guest
{
public string ID { get; set; }
public List<Model> Items { get; set; }
}
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public List<Guest> Items { get; set; }
public MainPage()
{
InitializeComponent();
Items = new List<Guest>();
Model model = new Model { Name = "Cole", Items = new List<Item> { new Item { Name = "aa" }, new Item { Name = "bb" } } };
Model model2 = new Model { Name = "Tom", Items = new List<Item> { new Item { Name = "cc" }, new Item { Name = "dd" } } };
Model model3 = new Model { Name = "Kevin", Items = new List<Item> { new Item { Name = "ee" }} };
Items.Add(new Guest { ID = "Guest 1", Items = new List<Model> { model } });
Items.Add(new Guest { ID = "Guest 2", Items = new List<Model> { model2 ,model3} });
this.BindingContext = this;
}
}
Test result
Related
I am using CollectionView to display data. I want CollectionView autoplay. However it doesn't work, even though I have it set to run automatically from time to time.. Don't know what I did wrong.
.xaml
<StackLayout HeightRequest="100">
<CollectionView x:Name="_data" HeightRequest="150" HorizontalScrollBarVisibility="Never" VerticalScrollBarVisibility="Never">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal" SnapPointsType="Mandatory" SnapPointsAlignment="Start" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<Image HorizontalOptions="Fill" Aspect="AspectFill" Source="{Binding Image}" WidthRequest="200"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
.xaml.cs
public Test2()
{
InitializeComponent();
LoadData();
}
protected async void LoadData()
{
var monkeyJson = await httpClient.GetStringAsync(monkeyUrl);
var monkeys = JsonConvert.DeserializeObject<Monkey[]>(monkeyJson);
foreach (var monkey in monkeys)
{
Monkeys.Add(monkey);
}
_listProd = Monkeys.ToList();
_data.ItemsSource = _listProd;
Device.StartTimer(TimeSpan.FromSeconds(5), (Func<bool>)(() =>
{
_data.ScrollTo(_listProd.Count + 2, -1, ScrollToPosition.Start, true);
return true;
}));
}
Note that I used CarouselView. However this time I want to use CollectionView
I searched for keywords related to CollectionView. However, there were no positive results. Looking forward to everyone's help. Thank you.
Update
...
MainThread.BeginInvokeOnMainThread(() =>
{
Device.StartTimer(TimeSpan.FromSeconds(5), (Func<bool>)(() =>
{
_data.ScrollTo(_listProd.Count + 2, -1, ScrollToPosition.Start, true);
return true;
}));
});
I have Binding Data problem in subpages. I have a Page that shows a list of products. When clicking on the product it will go to the Product Details subpage. Everything works fine:
Product.xaml
<RefreshView x:DataType="locals:DashboardViewModel" Padding="0" Command="{Binding LoadDashboardCommand}" IsRefreshing="{Binding IsBusy, Mode=OneWay}">
<StackLayout BindableLayout.ItemsSource="{Binding ProductNew}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Frame x:DataType="model:Product">
<StackLayout>
<Label FontSize="14" MaxLines="2" LineBreakMode="TailTruncation" HeightRequest="40" Text="{Binding Name}"/>
</StackLayout>
<Frame.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1"
Command="{Binding Source={RelativeSource AncestorType={x:Type locals:DashboardViewModel}}, Path=ProductTappedView}"
CommandParameter="{Binding .}" />
</Frame.GestureRecognizers>
</Frame>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</RefreshView>
Product.xaml.cs
DashboardViewModel dashboardViewModel;
public Product()
{
BindingContext = dashboardViewModel = new DashboardViewModel(Navigation);
dashboardViewModel.OnAppearing();
}
DashboardViewModel.cs
public class DashboardViewModel : BaseDashboardViewModel
{
public Command LoadDashboardCommand { get; }
public Command ProductTappedView { get; }
public ObservableCollection<Product> ProductNew { get; }
public DashboardViewModel(INavigation _navigation)
{
LoadDashboardCommand = new Command(async () => await ExecuteLoadDashboardCommand());
ProductNew = new ObservableCollection<Product>();
ProductTappedView = new Command<Product>(OnViewDetailProduct);
Navigation = _navigation;
}
private async void OnViewDetailProduct(Product detailProduct)
{
await Navigation.PushAsync(new ProductDetail(detailProduct));
}
............
}
Next in my Productdetail page show product details. I have Read more. When clicked it will redirect to another subpage: ContentDetailProd.xaml
ProductDetail.xaml
<ContentPage.BindingContext>
<locals:ViewDetailProductViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout Padding="15">
<Label Text="{Binding ProductNews.Name}" FontFamily="RobotoMedium" FontSize="18" TextTransform="Uppercase"/>
<Label Text="Read more"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding Source={RelativeSource AncestorType={x:Type locals:ViewDetailProductViewModel}}, Path=ContentProductTappedView}" CommandParameter="{Binding .}" />
</StackLayout.GestureRecognizers>
</StackLayout>
</ContentPage.Content>
ViewDetailProductViewModel.cs
public class ViewDetailProductViewModel : BaseDashboardViewModel
{
public Command ContentProductTappedView { get;}
public ViewDetailProductViewModel()
{
ProductNews = new Product();
ContentProductTappedView = new Command<Product>(OnViewContentDetailProduct);
}
private async void OnViewContentDetailProduct(Product detailProduct)
{
await Navigation.PushAsync(new ContentDetailProd(detailProduct));
}
}
ContentDetailProd.xaml.cs
public ContentDetailProd(Product detailProduct)
{
InitializeComponent();
//Load Readmore
}
However when I Debug it actually doesn't run on the event: ContentProductTappedView and as a result it doesn't redirect to the ContentDetailProd.xaml page. I've tried everything but still can't solve the problem.
Looking forward to everyone's help. Thanks
If you want to pass the data through a Page Constructor, you could refer to the link below. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical#passing-data-when-navigating
I changed like this based on the documentation #Wendy Zang - MSFT provided. Everything looks good.
ProductDetail.xaml
<StackLayout x:Name="_readmore" Padding="15">
<Label Text="{Binding ProductNews.Name}" FontFamily="RobotoMedium" FontSize="18" TextTransform="Uppercase"/>
<Label x:Name="_content" MaxLines="2" LineBreakMode="TailTruncation" Text="{Binding ProductNews.Contents}" FontFamily="RobotoMedium" FontSize="18" TextTransform="Uppercase"/>
<Label Text="Read more"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="_readmore_Tapped" />
</StackLayout.GestureRecognizers>
</StackLayout>
ProductDetail.xaml.cs
private async void _readmore_Tapped(object sender, EventArgs e)
{
var contentget = _content.Text;
var detailProduct = new Product
{
Contents = contentget,
};
var detailContentPage = new ContentDetailProd(detailProduct);
detailContentPage.BindingContext = detailProduct;
await Navigation.PushAsync(detailContentPage);
}
ContentDetailProd.xaml.cs
public ContentDetailProd(Product detailProduct)
{
InitializeComponent();
//Load Readmore
if (detailProduct != null)
{
_contentmore.Text = detailProduct.Contents;
}
}
I have the following list:
<StackLayout BindableLayout.ItemsSource="{Binding NotificationList}" Margin="0,10,0,10" >
<BindableLayout.ItemTemplate>
<DataTemplate> <StackLayout Style="{StaticResource StacklayoutAStyle}">
<Label Text="{Binding notificationLabel}" VerticalOptions="Center" VerticalTextAlignment="Center"/>
<Switch IsToggled="{Binding isNotificationToggled, Mode=TwoWay}" VerticalOptions="Center" HorizontalOptions="End" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
I'd like to get the notificationLabel text when I toggle the corresponding switch in the list. My ViewModel looks like this:
private ObservableCollection<NotificationModel> _notificationList = new ObservableCollection<NotificationModel>
{
new NotificationModel { notificationLabel = "Notification1", isNotificationToggled = true },
new NotificationModel { notificationLabel = "Notification2", isNotificationToggled = false },
new NotificationModel { notificationLabel = "Notification3", isNotificationToggled = false },
new NotificationModel { notificationLabel = "Notification4", isNotificationToggled = true },
};
public ObservableCollection<NotificationModel> NotificationList
{
get { return _notificationList; }
set { SetProperty(ref _notificationList, value); }
}
When I toggled Notification 3 for example, it switches to on (true), but how can I capture that event with "Notification 3"?
You can use Switch.Toggled to listen for toggled event,then get the BindingContext (NotificationModel),then determine which Switch is turned on according to NotificationModel.notificationLabel.
<StackLayout BindableLayout.ItemsSource="{Binding NotificationList}" Margin="0,10,0,10" >
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Style="{StaticResource StacklayoutAStyle}">
<Label Text="{Binding notificationLabel}" VerticalOptions="Center" VerticalTextAlignment="Center"/>
<Switch IsToggled="{Binding isNotificationToggled, Mode=TwoWay}" VerticalOptions="Center" HorizontalOptions="End" Toggled="Switch_Toggled" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
in your page.xaml.cs:
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
Switch sw = sender as Switch;
NotificationModel notificationModel = (NotificationModel)sw.BindingContext;
if (e.Value)
{
switch (notificationModel.notificationLabel)
{
case "Notification1":
//do something
break;
case "Notification2":
//do something
break;
case "Notification3":
//do something
break;
case "Notification4":
//do something
break;
}
}
}
I cannot get the image buttons to move the carousel image to the next in the list. when I click the imagebutton and step through the code the Position is changing properly, but does not move the carousel in the view. Any help would be greatly appreciated. Here is the code for both my View and View Model:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Das_Local.Views.MainPage"
xmlns:vm="clr-namespace:Das_Local.ViewModels"
Title="{Binding Title}">
<ContentPage.BindingContext>
<vm:MainViewModel/>
</ContentPage.BindingContext>
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="Accent">#96d1ff</Color>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<StackLayout VerticalOptions="Start">
<CarouselView ItemsSource="{Binding Images}" HorizontalOptions="FillAndExpand" HeightRequest="375">
<CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ImageUrl}" Aspect="AspectFill" />
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
<StackLayout>
<Label Text="Whats New in the Local" FontSize="Title" HorizontalTextAlignment="Center" Margin="0,10,0,-10"/>
<Grid HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<CarouselView Grid.Row="1" HorizontalOptions="FillAndExpand" ItemsSource="{Binding Articles}">
<CarouselView.ItemTemplate>
<DataTemplate>
<Frame HasShadow="True">
<StackLayout Margin="0,0">
<Label Text="{Binding ArticleTitle}" FontAttributes="Bold" FontSize="Title"/>
<Label Text="{Binding ArticleTextBody}" FontSize="Body" />
</StackLayout>
</Frame>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<ImageButton Grid.Row="1" Source="left.png" Aspect="Fill" HorizontalOptions="Start" VerticalOptions="Center" BackgroundColor="Transparent"
HeightRequest="35" WidthRequest="35" Command="{Binding ChangePositionCommand}" CommandParameter="L"/>
<ImageButton Grid.Row="1" Source="right.png" Aspect="Fill" HorizontalOptions="End" VerticalOptions="Center" BackgroundColor="Transparent"
HeightRequest="35" WidthRequest="35" Command="{Binding ChangePositionCommand}" CommandParameter="R"/>
</Grid>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
--------------------------------------------------------------------------------------------------
using System.Collections.ObjectModel;
using System.Windows.Input;
using Das_Local.Models;
using Xamarin.Forms;
namespace Das_Local.ViewModels
{
class MainViewModel : BaseViewModel
{
public MainViewModel()
{
Title = "SMART 29";
images = GetCarouselImages();
articles = GetCarouselArticles();
ChangePositionCommand = new Command(ChangePosition);
}
private ObservableCollection<CarouselImages> images;
public ObservableCollection<CarouselImages> Images
{
get { return images; }
set
{
images = value;
OnPropertyChange();
}
}
private ObservableCollection<CarouselImages> GetCarouselImages()
{
return new ObservableCollection<CarouselImages>
{
new CarouselImages { ImageUrl= "anglewrappedduct.jpg" },
new CarouselImages { ImageUrl = "ductrun.jpg" },
new CarouselImages { ImageUrl = "offset.jpg" },
new CarouselImages { ImageUrl = "panels.jpg" }
};
}
ObservableCollection<CarouselArticles> articles;
public ObservableCollection<CarouselArticles> Articles
{
get { return articles; }
set
{
articles = value;
OnPropertyChange();
}
}
private ObservableCollection<CarouselArticles> GetCarouselArticles()
{
return new ObservableCollection<CarouselArticles>
{
new CarouselArticles { ArticleTitle= "This Title", ArticleTextBody = "Body of the article will go here"},
new CarouselArticles { ArticleTitle = "Another Title", ArticleTextBody = "Just what I need more text for the article body" },
new CarouselArticles { ArticleTitle = "My Third Title", ArticleTextBody = "I am getting burned out on writing random things"},
new CarouselArticles { ArticleTitle = "Bravo", ArticleTextBody = "whats the deal with these free channels"}
};
}
public ICommand ChangePositionCommand { get; set; }
private CarouselArticles selectedarticle;
public CarouselArticles SelectedArticle
{
get { return selectedarticle; }
set
{
selectedarticle = value;
OnPropertyChange();
}
}
private int position;
public int Position
{
get { return position; }
set
{
position = value;
selectedarticle = articles[position];
OnPropertyChange(nameof(SelectedArticle));
}
}
private void ChangePosition(object obj)
{
string direction = (string)obj;
if (direction == "L")
{
if (position == 0)
{
Position = articles.Count - 1;
return;
}
Position -= 1;
}
else if (direction == "R")
{
if (position == articles.Count - 1)
{
Position = 0;
return;
}
Position += 1;
}
}
}
}
I've tried a few different methods based on the official documentation and from debugging my if/else condition is firing correctly, but I can't seem to get my image to change.
As you can see, I've tried two methods here but my understanding is that ImageSource.FromFile isn't needed because of implicit conversion.
Here's the 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>
And the code behind:
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");
}
}
}