Connected Animation in UWP - c#

Class Icons.cs:
public class Icon
{
public int IconID { get; set; }
public string Title { get; set; }
public string Room { get; set; }
public string ImageCover { get; set; }
}
public class IconManager
{
public static List<Icon> GetIcons()
{
var Icons = new List<Icon>();
Icons.Add(new Icon { IconID = 1, Title = "Mr.Ha", Room = "611-Room", ImageCover = "Assets/Ha.jpg" });
Icons.Add(new Icon { IconID = 2, Title = "Mr.Synh", Room = "611-Room", ImageCover = "Assets/Synh.jpg" });
return Icons;
}
}
Class MainPage:
public MainPage()
{
this.NavigationCacheMode = NavigationCacheMode.Enabled;
this.InitializeComponent();
Icons = IconManager.GetIcons();
}
private void ForegroundElement_ItemClick(object sender, ItemClickEventArgs e)
{
var icon = (Icon)e.ClickedItem;
var tittle = icon.Title;
ForegroundElement.PrepareConnectedAnimation("ca1", icon, "ConnectedElement");
switch (tittle)
{
case "Mr.Ha":
Frame.Navigate(typeof(BlankPage1));
break;
}
}
private async void ForegroundElement_Loaded(object sender, RoutedEventArgs e)
{
if(Icons != null)
{
ForegroundElement.ScrollIntoView(Icons, ScrollIntoViewAlignment.Default);
ForegroundElement.UpdateLayout();
ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().GetAnimation("ca2");
if (animation != null)
{
await ForegroundElement.TryStartConnectedAnimationAsync(
animation, Icons, "ConnectedElement");
}
}
Class BlankPage1:
public BlankPage1()
{
this.NavigationCacheMode = NavigationCacheMode.Enabled;
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ConnectedAnimation imageAnimation = ConnectedAnimationService.GetForCurrentView().GetAnimation("ca1");
if (imageAnimation != null)
{
imageAnimation.TryStart(TargetElement);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("ca2", TargetElement);
Frame.Navigate(typeof(MainPage));
}
There something wrong with my Animation when I "Navigate Back" to the MainPage. The picture just stay still there then it's disappear, while the listview is done loaded !!My navigation from MainPage to BlankPage1 is fine though!!

Your second connected animation is called ca2, but you are using ca1 in the MainPage's ForegroundElement_Loaded event handler. Because of this, the animation cannot be connected, which results in the behavior you are seeing.

Related

C# Xamarin.Android Calling Dialogfragment from recyclerView

I would like to display diaglog fragment by clicking a button/imageView (By clicking the sAddButton)from recyclerView. But when I clicked the button nothing shows up.
How do I achieve that here is my code.
My RecyclerView Adapter
I added an pulic eventhandler in myadapter (StockInItemClick)
then created a delegate method to invoke the eventhandler(OnStockInClick)
then pass the delegate method as parameter on my adapterview holder
then Implement the delegate method on my adapterview holder.
internal class StocksAdapter : RecyclerView.Adapter
{
public event EventHandler<StocksAdapterClickEventArgs> ItemClick;
public event EventHandler<StocksAdapterClickEventArgs> ItemLongClick;
public event EventHandler<StocksAdapterClickEventArgs> StockInItemClick;
List<Products> items;
public StocksAdapter(List<Products> data)
{
items = data;
}
// Create new views (invoked by the layout manager)
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
//Setup your layout here
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.stock_rView, parent, false);
//var id = Resource.Layout.__YOUR_ITEM_HERE;
//itemView = LayoutInflater.From(parent.Context).
// Inflate(id, parent, false);
var vh = new StocksAdapterViewHolder(itemView, OnClick, OnLongClick, OnStockInClick);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
{
var holder = viewHolder as StocksAdapterViewHolder;
//holder.TextView.Text = items[position];
holder.sNameText.Text = items[position].PName;
holder.sQtyText.Text = items[position].QtyonHand;
}
public override int ItemCount => items.Count;
void OnClick(StocksAdapterClickEventArgs args) => ItemClick?.Invoke(this, args);
void OnLongClick(StocksAdapterClickEventArgs args) => ItemLongClick?.Invoke(this, args);
void OnStockInClick(StocksAdapterClickEventArgs args) => StockInItemClick?.Invoke(this, args);
}
public class StocksAdapterViewHolder : RecyclerView.ViewHolder
{
//public TextView TextView { get; set; }
public TextView sNameText { get; set; }
public TextView sQtyText { get; set; }
public ImageButton sAddButton { get; set; }
public StocksAdapterViewHolder(View itemView, Action<StocksAdapterClickEventArgs> clickListener,
Action<StocksAdapterClickEventArgs> longClickListener,
Action<StocksAdapterClickEventArgs> stockInClickListener) : base(itemView)
{
//TextView = v;
sNameText = (TextView)itemView.FindViewById(Resource.Id.sNameTView);
sQtyText = (TextView)itemView.FindViewById(Resource.Id.qtyonhandTView);
sAddButton = (ImageButton)itemView.FindViewById(Resource.Id.addButton);
itemView.Click += (sender, e) => clickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });
itemView.LongClick += (sender, e) => longClickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });
sAddButton.Click += (sender, e) => longClickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });
}
}
public class StocksAdapterClickEventArgs : EventArgs
{
public View View { get; set; }
public int Position { get; set; }
}
my code in activity
I implement the eventhandler
then on the event onlcick event(Adapter_StockInItemClick) I called the
dialog fragment.
private void SetupRecyclerView()
{
stockRView.SetLayoutManager(new Android.Support.V7.Widget.LinearLayoutManager(stockRView.Context));
adapter = new StocksAdapter(StockList);
adapter.StockInItemClick += Adapter_StockInItemClick;
stockRView.SetAdapter(adapter);
}
private void Adapter_StockInItemClick(object sender, StocksAdapterClickEventArgs e)
{
Products thisproducts = StockList[e.Position];
addNewStockInProductsFragment = new AddNewStockInProductsFragment(thisproducts);
var trans = SupportFragmentManager.BeginTransaction();
addNewStockInProductsFragment.Show(trans, "StockIn");
}
Found the problem
sAddButton.Click += (sender, e) => longClickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });
shouldbe (deleteClickListener) not (longClickListener)
sAddButton.Click += (sender, e) => deleteClickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });

How fix delete row in WPF

I trying add a delete row but when im editing a some row(delete the word or symbol), the row will remove. i want delete row. if the row in editing mode and if i press a delete then the row will be remove.
public partial class MainWindow : Window
{
ObservableCollection<Employee> johnSmith = new ObservableCollection<Employee>();[enter image description here][1]
public MainWindow()
{
InitializeComponent();
dataGrid.ItemsSource = johnSmith;
}
public class Employee
{
public string employeeID { get; set; }
public string employeeName { get; set; }
public string employeeAddress { get; set; }
public string employeeCity { get; set; }
public string employeeState { get; set; }
}
private void AddNewEmployeeClicked(object sender, RoutedEventArgs e)
{
johnSmith.Add(new Employee()
{
employeeID = IDTB.Text,
employeeAddress = AdressTB.Text,
employeeCity = CityTB.Text,
employeeName = NameTB.Text,
employeeState = StateTB.Text
});
}
private void datagrid_loadingrow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
private void deletegrid_keydown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (dataGrid.SelectedItem != null)
{
johnSmith.Remove(item);
}
}
}
If you want to prevent row deletion, you can use CanUserDeleteRows="False".
If you want to perform some custom handling related to keyboard input, you can use PreviewKeyDown event and subscribe your handling method.
PreviewKeyDown="DataGrid_PreviewKeyDown"
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete || e.Key == Key.Back)
{
e.Handled = true;
//put some additional handling if you need
}
}

Error press button xamarin.forms using reactiveui

I am using ReactiveUI in a Xamarin.Forms project but when I press a button I get the error: "Only the original thread that created a view hierarchy can touch its views. '"
Here is my code.
App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
AppShell.xaml.cs
public partial class AppShell : Shell
{
Random rand = new Random();
Dictionary<string, Type> routes = new Dictionary<string, Type>();
public Dictionary<string, Type> Routes { get { return routes; } }
public AppShell()
{
InitializeComponent();
RegisterRoutes();
BindingContext = this;
}
void RegisterRoutes()
{
routes.Add("monkeydetails", typeof(HomeView));
foreach (var item in routes)
{
Routing.RegisterRoute(item.Key, item.Value);
}
}
void OnNavigating(object sender, ShellNavigatingEventArgs e)
{
// Cancel any back navigation
//if (e.Source == ShellNavigationSource.Pop)
//{
// e.Cancel();
//}
}
void OnNavigated(object sender, ShellNavigatedEventArgs e)
{
}
}
By default you go to the HomeView view
HomeView.xaml
<Button Text="Pulsar" x:Name="Boton"></Button>
HomeView.xaml.cs
public partial class HomeView : ReactiveContentPage<HomeViewModel>
{
protected CompositeDisposable ControlBindings { get; } = new CompositeDisposable();
public ReactiveCommand<Unit, Unit> Navigate { get; private set; }
public HomeView()
{
InitializeComponent();
this.ViewModel = new HomeViewModel();
this.BindCommand(ViewModel, vm => vm.Navigate, view => view.Boton);
}
}
HomeViewModel.cs
public class HomeViewModel : ViewModelBase, IRoutableViewModel
{
int prueb = 0;
public HomeViewModel()
{
Navigate = ReactiveCommand.CreateFromTask(async() =>
{
await hola();
});
}
public async Task hola()
{
}
public string prueba()
{
return prueb.ToString();
}
public IObservable<string> NumberStream { get; }
public string UrlPathSegment => "Number Stream Page";
public IScreen HostScreen { get; }
public override string Id => "Pass Parameter";
public ReactiveCommand<Unit, Unit> Navigate { get; private set; }
}
I can't understand why the error appears when I press the button

Showing a few popupmenu on one click button in RecyclerView Xamarin Android

I have problem with showing popupmenu. When I clicked on ImageButton , on display shown a few popupmenu(for different elements - in click listener of ClickedPopUpMenu I show toast with position).
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Android.App;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using DexpensMobile.Core.Models;
using DexpensMobile.Core.Services;
using DexpensMobile.Core.Services.AuthService;
using DexpensMobile.Droid.Screens;
using UniversalImageLoader.Core;
using UniversalImageLoader.Core.Display;
using PopupMenu = Android.Widget.PopupMenu;
namespace DexpensMobile.Droid.Adapters
{
public class PostListAdapter : RecyclerView.Adapter
{
protected Activity context = null;
protected IList<PostModel> tasks = new List<PostModel>();
ImageLoader _imageLoader;
private int popPosition;
private int TYPE_HEADER = 0;
private int TYPE_ITEM = 1;
public event EventHandler<int> ItemClick;
public PostListAdapter(IList<PostModel> tasks, Activity context)
{
this.tasks = tasks;
_imageLoader = ImageLoader.Instance;
this.context = context;
}
public PostListAdapter() { }
public override long GetItemId(int position)
{
return position;
}
public override int GetItemViewType(int position)
{
if (IsPositionHeader(position))
return Resource.Layout.wall_header;
return Resource.Layout.ItemPost;
}
private bool IsPositionHeader(int position)
{
return position == 0;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if (holder.GetType() == typeof(PostViewHodler))
{
PostViewHodler vh = holder as PostViewHodler;
var item = tasks[position-1];
var _userData = ServiceLocator.GetService<IAuthService>().GetUserData();
vh.ShowPopUpMenuButton.Click += (sender, e) => button_Click(sender, e, position);
var tempText = item.Text;
if (tempText.EndsWith("<br/>"))
tempText = tempText.Substring(0, tempText.Length - 5);
tempText = tempText.Replace("<br/>", "\n");
tempText = Regex.Replace(tempText, #"^\s+$[\n]*", "", RegexOptions.Multiline);
vh.PostText.Text = tempText;
vh.OwnerFullName.Text = item.CreatorName;
vh.DateText.Text = item.CreatedDate.ToString(CultureInfo.InvariantCulture);
vh.LikeText.Text = item.Like.LikesCount > 0
? item.Like.LikesCount.ToString()
: "";
vh.CommentText.Text = item.Comments.Any() ? item.Comments.Count().ToString() : "";
vh.RepostText.Text = item.SharedCount > 0 ? item.SharedCount.ToString() : "";
var options =
new DisplayImageOptions.Builder()
.CacheInMemory(false)
.CacheOnDisk(false)
.Displayer(new RoundedBitmapDisplayer(500))
.Build();
_imageLoader.DisplayImage(item.AvatarUrl, vh.AvatarProfile, options);
}
else if (holder.GetType() == typeof(PostViewHodlerHeader))
{
PostViewHodlerHeader vh = holder as PostViewHodlerHeader;
vh.OwnerNameHeader.Text = "Hi from header :-)";
}
}
private void button_Click(object sender, EventArgs eventArgs, int position)
{
ImageButton im = sender as ImageButton;
PopupMenu menu = new PopupMenu(im.Context, im);
menu.Inflate(Resource.Menu.popup_menu);
//if (item.ApplicationUserId != _userData.UserId)
//{
menu.Menu.FindItem(Resource.Id.editPopUp).SetVisible(false);
menu.Menu.FindItem(Resource.Id.deletePopUp).SetVisible(false);
//}
menu.Show();
Toast.MakeText(context, position.ToString(), ToastLength.Long).Show();
menu.MenuItemClick += (o, args) => PopUpMenuClickListener(o, args, position);
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = null;
if (viewType == Resource.Layout.wall_header)
{
itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.wall_header, parent, false);
PostViewHodlerHeader vhHeader = new PostViewHodlerHeader(itemView);
return vhHeader;
}
else if(viewType == Resource.Layout.ItemPost)
{
itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ItemPost, parent, false);
PostViewHodler vhBody = new PostViewHodler(itemView, OnClick, tasks);
return vhBody;
}
return null;
}
private void OnClick(int position)
{
if (ItemClick != null)
ItemClick(this, position);
}
public override int ItemCount
{
get { return tasks.Count +1; }
}
private void PopUpMenuClickListener(object sender, PopupMenu.MenuItemClickEventArgs e, int position)
{
switch(e.Item.ItemId)
{
case Resource.Id.copyLinkPopUp:
break;
case Resource.Id.editPopUp:
((WallActivity)context).EditPost(popPosition);
break;
case Resource.Id.deletePopUp:
((WallActivity)context).DeleteItem(popPosition);
break;
}
}
}
public class PostViewHodler : RecyclerView.ViewHolder
{
public TextView OwnerFullName { get; private set; }
public ImageView AvatarProfile { get; private set; }
public TextView DateText { get; private set; }
public TextView PostText { get; private set; }
public TextView LikeText { get; private set; }
public TextView CommentText { get; private set; }
public TextView RepostText { get; private set; }
public ImageButton ShowPopUpMenuButton { get; private set; }
public PostViewHodler(View itemView, Action<int> listener, IList<PostModel> posts) : base(itemView)
{
OwnerFullName = itemView.FindViewById<TextView>(Resource.Id.UserName);
AvatarProfile = itemView.FindViewById<ImageView>(Resource.Id.postAva);
DateText = itemView.FindViewById<TextView>(Resource.Id.dateText);
PostText = itemView.FindViewById<TextView>(Resource.Id.postText);
LikeText = itemView.FindViewById<TextView>(Resource.Id.likeText);
CommentText = itemView.FindViewById<TextView>(Resource.Id.commentText);
RepostText = itemView.FindViewById<TextView>(Resource.Id.repostText);
ShowPopUpMenuButton = itemView.FindViewById<ImageButton>(Resource.Id.popupMenuButton);
itemView.Click += (sender, e) => listener(base.Position);
}
}
public class PostViewHodlerHeader : RecyclerView.ViewHolder
{
public TextView OwnerNameHeader{ get; private set; }
public ImageView AvatarProfileHeader { get; private set; }
public PostViewHodlerHeader(View itemView) : base(itemView)
{
OwnerNameHeader = itemView.FindViewById<TextView>(Resource.Id.details);
AvatarProfileHeader = itemView.FindViewById<ImageView>(Resource.Id.avatar_image);
}
}
}
I think problem bind with recycling elements.
Video link with problem.
How to show only one popupmenu only for current item in recyclerview? Why this problem occur?

How to get all cell values form a Property Grid c#

I have a property Grid as follows:
I want to copy the complete content of the property grid to a data grid view(dataGeriView1) when submit button is clicked.
How to do this?
Please help.
private void Submit_Click(object sender, EventArgs e)
{
//propertyGrid1.SelectedObject = this;
dataGridView1.Columns.Add("Property", "Property");
dataGridView1.Columns.Add("Value", "Value");
GridItem gi = propertyGrid1.SelectedGridItem;
while (gi.Parent != null)
gi = gi.Parent;
foreach (GridItem item in gi.GridItems)
ParseGridItems(item); //recursive
dataGridView1.Sort(dataGridView1.Columns["Property"], ListSortDirection.Ascending);
}
private void ParseGridItems(GridItem gi)
{
if (gi.GridItemType == GridItemType.Category)
foreach (GridItem item in gi.GridItems)
ParseGridItems(item);
dataGridView1.Rows.Add(gi.Label, gi.Value);
}
Adapted from https://stackoverflow.com/a/12109186/1163434
Below is a sample snippet i have created to solve the above issue. Create a DataGridview by adding Columns Name,Age,Email,Phone.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Student std = new Student {Name = "Vimal" , Phone = "PhoneValue", Email="mymail",Age=24};
propertyGrid1.SelectedObject= std;
}
private void button1_Click(object sender, EventArgs e)
{
int index = dataGridView1.Rows.Count - 1;
Student std = (Student)propertyGrid1.SelectedObject;
dataGridView1.Rows[index].Cells["Name"].Value = std.Name;
dataGridView1.Rows[index].Cells["Age"].Value = std.Age;
dataGridView1.Rows[index].Cells["Email"].Value = std.Email;
dataGridView1.Rows[index].Cells["Phone"].Value = std.Phone;
}
}
public class Student
{
public int Age { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}

Categories