This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
327 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...in.Forms.Controls/GalleryPages/CollectionViewGalleries/ItemSizeGalleries/ChatExample.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:chat="clr-namespace:Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.ItemSizeGalleries" | ||
x:Class="Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.ItemSizeGalleries.ChatExample"> | ||
|
||
<ContentPage.Resources> | ||
<ResourceDictionary> | ||
<DataTemplate x:Key="Local"> | ||
<Grid> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="*"></ColumnDefinition> | ||
<ColumnDefinition Width="2*"></ColumnDefinition> | ||
</Grid.ColumnDefinitions> | ||
<Frame BorderColor="Green" BackgroundColor="LightGreen" CornerRadius="5" Grid.Column="1"> | ||
<StackLayout Margin="2"> | ||
<Label Text="{Binding Text}" LineBreakMode="WordWrap" FontSize="10" HorizontalTextAlignment="End" /> | ||
</StackLayout> | ||
</Frame> | ||
</Grid> | ||
</DataTemplate> | ||
|
||
<DataTemplate x:Key="Remote"> | ||
<Grid Padding="5,0,5,0"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="2*"></ColumnDefinition> | ||
<ColumnDefinition Width="*"></ColumnDefinition> | ||
</Grid.ColumnDefinitions> | ||
<Frame BorderColor="Blue" BackgroundColor="LightBlue" CornerRadius="5"> | ||
<StackLayout Margin="2"> | ||
<Label Text="{Binding Text}" LineBreakMode="WordWrap" FontSize="10" HorizontalTextAlignment="Start" /> | ||
</StackLayout> | ||
</Frame> | ||
</Grid> | ||
</DataTemplate> | ||
|
||
<chat:ChatTemplateSelector x:Key="ChatTemplateSelector" | ||
LocalTemplate="{StaticResource Local}" | ||
RemoteTemplate="{StaticResource Remote}"/> | ||
|
||
</ResourceDictionary> | ||
</ContentPage.Resources> | ||
|
||
<ContentPage.Content> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto"/> | ||
<RowDefinition Height="*"/> | ||
</Grid.RowDefinitions> | ||
|
||
<StackLayout Orientation="Horizontal"> | ||
<Button x:Name="AppendRandomSizedItem" Text="Append Random Message" Margin="2" /> | ||
<Button x:Name="Clear" Text="Clear" Margin="2" /> | ||
<Button x:Name="Lots" Text="Add 1000 Messages" Margin="2" /> | ||
</StackLayout> | ||
|
||
<CollectionView ItemsSource="{Binding ChatMessages}" | ||
ItemTemplate="{StaticResource ChatTemplateSelector}" | ||
ItemSizingStrategy="MeasureAllItems" | ||
Grid.Row="1"> | ||
<CollectionView.ItemsLayout> | ||
<LinearItemsLayout Orientation="Vertical" ItemSpacing="5"/> | ||
</CollectionView.ItemsLayout> | ||
</CollectionView> | ||
|
||
</Grid> | ||
</ContentPage.Content> | ||
</ContentPage> |
115 changes: 115 additions & 0 deletions
115
...Forms.Controls/GalleryPages/CollectionViewGalleries/ItemSizeGalleries/ChatExample.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using Xamarin.Forms.Xaml; | ||
|
||
namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.ItemSizeGalleries | ||
{ | ||
[XamlCompilation(XamlCompilationOptions.Compile)] | ||
public partial class ChatExample : ContentPage | ||
{ | ||
Random _random = new Random(DateTime.Now.Millisecond); | ||
ChatExampleViewModel _vm = new ChatExampleViewModel(); | ||
|
||
public ChatExample() | ||
{ | ||
InitializeComponent(); | ||
BindingContext = _vm; | ||
|
||
AppendRandomSizedItem.Clicked += AppendRandomChatMessage; | ||
Clear.Clicked += ClearMessages; | ||
Lots.Clicked += LotsOfMessages; | ||
} | ||
|
||
void AppendRandomChatMessage(object sender, EventArgs e) | ||
{ | ||
_vm.ChatMessages.Add(GenerateRandomMessage()); | ||
} | ||
|
||
void LotsOfMessages(object sender, EventArgs e) | ||
{ | ||
var newVm = new ChatExampleViewModel(GenerateMessages(1000)); | ||
_vm = newVm; | ||
BindingContext = _vm; | ||
} | ||
|
||
void ClearMessages(object sender, EventArgs e) | ||
{ | ||
_vm.ChatMessages.Clear(); | ||
} | ||
|
||
ChatMessage GenerateRandomMessage() | ||
{ | ||
const string lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut " | ||
+ "labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip " | ||
+ "ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat " | ||
+ "nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; | ||
|
||
var local = _random.Next(0, 2) == 1; | ||
|
||
var textLength = _random.Next(0, lorem.Length - 1); | ||
|
||
var text = lorem.Substring(0, textLength); | ||
|
||
return new ChatMessage(text, local); | ||
} | ||
|
||
IEnumerable<ChatMessage> GenerateMessages(int count) | ||
{ | ||
for (int n = 0; n < count; n++) | ||
{ | ||
yield return GenerateRandomMessage(); | ||
} | ||
} | ||
} | ||
|
||
public class ChatExampleViewModel | ||
{ | ||
public ObservableCollection<ChatMessage> ChatMessages { get; } | ||
|
||
public ChatExampleViewModel() | ||
{ | ||
ChatMessages = new ObservableCollection<ChatMessage>(); | ||
} | ||
|
||
public ChatExampleViewModel(IEnumerable<ChatMessage> chatMessages) | ||
{ | ||
ChatMessages = new ObservableCollection<ChatMessage>(chatMessages); | ||
} | ||
} | ||
|
||
public class ChatMessage | ||
{ | ||
public bool IsLocal { get; set; } | ||
public string Text { get; set; } | ||
|
||
public ChatMessage(string text) : this(text, true) { } | ||
|
||
public ChatMessage(string text, bool isLocal) | ||
{ | ||
IsLocal = isLocal; | ||
Text = text; | ||
} | ||
} | ||
|
||
class ChatTemplateSelector : DataTemplateSelector | ||
{ | ||
public DataTemplate LocalTemplate { get; set; } | ||
public DataTemplate RemoteTemplate { get; set; } | ||
|
||
protected override DataTemplate OnSelectTemplate(object item, BindableObject container) | ||
{ | ||
if (item is ChatMessage message) | ||
{ | ||
if (message.IsLocal) | ||
{ | ||
return LocalTemplate; | ||
} | ||
|
||
return RemoteTemplate; | ||
} | ||
|
||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.