我正在我的 Xamarin Forms 應用程式的 C# 代碼中構建一個網格。我想為用戶提供在一行上滑動并有一個 swipeview 托盤顯示的能力。我試圖按照這里的檔案 - https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/swipeview。
下面是我的代碼:
private void AddSwipeControl(int pi_RowNumber)
{
try
{
//Add the swipeview
//Add a swip control to the and have it go all the way across. See if this works
// SwipeItems
SwipeItem tobj_DeleteSwipeItem = new SwipeItem
{
Text = "Delete",
BackgroundColor = Color.LightGreen
};
tobj_DeleteSwipeItem.Invoked = tobj_DeleteSwipeItem_Invoked; ;
List<SwipeItem> tobj_swipeItems = new List<SwipeItem>() { tobj_DeleteSwipeItem };
// SwipeView content
Label swipeContent = new Label()
{
BackgroundColor = Color.Pink,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
var tobj_SwipeGestureRecognizer = new SwipeGestureRecognizer() { Direction = SwipeDirection.Right };
tobj_SwipeGestureRecognizer.Swiped = tobj_SwipeGestureRecognizer_Swiped;
swipeContent.GestureRecognizers.Add(tobj_SwipeGestureRecognizer);
SwipeView swipeView = new SwipeView
{
LeftItems = new SwipeItems(tobj_swipeItems) { Mode = SwipeMode.Reveal },
Content = swipeContent
};
//Add the swipeview to the grid
grdDataGrid.Children.Add(swipeView, 0, pi_RowNumber);
Grid.SetColumnSpan(swipeView, grdDataGrid.ColumnDefinitions.Count);
}
catch (Exception ex)
{
SharedErrorHandler.ProcessException(ex);
}
}
private void tobj_SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
{
try
{
if (e.Direction == SwipeDirection.Right)
{
var tobj_SwipeView = ((Label)sender).Parent as SwipeView;
tobj_SwipeView.Open(OpenSwipeItem.LeftItems);
}
}
catch (Exception ex)
{
SharedErrorHandler.ProcessException(ex);
}
}
我可以在我想要的網格行中看到我的滑動視圖的內容,并且標簽控制元件的滑動事件按預期觸發。但是,即使我嘗試使用 tobj_SwipeView.Open(OpenSwipeItem.LeftItems); 以編程方式打開它,滑動托盤也永遠不會打開。這使我認為我在構建滑動視圖時做錯了。我已經在 Android 和 UWP 上進行了測驗。任何想法我做錯了什么?
更新:所以這似乎只是對 UWP 的挑戰。下面是我更新的代碼。我在螢屏上有一個按鈕來運行 Button_Clicked 事件。
private void tobj_SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
{
try
{
if (e.Direction == SwipeDirection.Right)
{
var tobj_SwipeView = ((Label)sender).Parent as SwipeView;
tobj_SwipeView.Open(OpenSwipeItem.LeftItems);
}
}
catch (Exception ex)
{
//SharedErrorHandler.ProcessException(ex);
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
private void Button_Clicked(object sender, EventArgs e)
{
try
{
//First add two rows
grdDataGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grdDataGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
//First add some data columns to the grid
grdDataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto});
grdDataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
grdDataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
//Add data to each row
grdDataGrid.Children.Add(new Label() { Text = "Row 0 Col 0" }, 0 , 0);
grdDataGrid.Children.Add(new Label() { Text = "Row 0 Col 1" }, 1, 0);
grdDataGrid.Children.Add(new Label() { Text = "Row 0 Col 2" }, 2, 0);
grdDataGrid.Children.Add(new Label() { Text = "Row 1 Col 0" }, 0, 1);
grdDataGrid.Children.Add(new Label() { Text = "Row 1 Col 1" }, 1, 1);
grdDataGrid.Children.Add(new Label() { Text = "Row 1 Col 2" }, 2, 1);
var ti_RowNumber = -1;
foreach (RowDefinition tobj_Row in grdDataGrid.RowDefinitions)
{
ti_RowNumber = 1;
//Add the swipeview
//Add a swip control to the and have it go all the way across. See if this works
// SwipeItems
SwipeItem tobj_DeleteSwipeItem = new SwipeItem
{
Text = "Delete",
BackgroundColor = Color.LightGreen
};
tobj_DeleteSwipeItem.Invoked = OnDeleteSwipeItemInvoked; ;
List<SwipeItem> tobj_swipeItems = new List<SwipeItem>() { tobj_DeleteSwipeItem }; //comment out
// SwipeView content
Label swipeContent = new Label()
{
BackgroundColor = Color.Pink,
Text = "test",
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
var tobj_SwipeGestureRecognizer = new SwipeGestureRecognizer() { Direction = SwipeDirection.Right };
tobj_SwipeGestureRecognizer.Swiped = tobj_SwipeGestureRecognizer_Swiped;
swipeContent.GestureRecognizers.Add(tobj_SwipeGestureRecognizer);
SwipeView swipeView = new SwipeView
{
LeftItems = new SwipeItems(tobj_swipeItems) { Mode = SwipeMode.Reveal },
Content = swipeContent
};
//Add the swipeview to the grid
grdDataGrid.Children.Add(swipeView, 0, ti_RowNumber);
Grid.SetColumnSpan(swipeView, grdDataGrid.ColumnDefinitions.Count);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
這似乎在 iOS 和 Android 上運行良好,但在 UWP 上,標簽的滑動事件會觸發,但滑動托盤永遠不會打開。這是一個已知的 UWP 挑戰嗎?
uj5u.com熱心網友回復:
我根據您的代碼創建了一個簡單的演示,它在我這邊可以正常作業。
您可以參考以下代碼:
<StackLayout Margin="20">
<Grid x:Name="grdDataGrid" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions >
<RowDefinition Height="60" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
<Button Text="add" Clicked="Button_Clicked" />
</StackLayout>
C#代碼是:
private void AddSwipeControl()
{
try
{
//Add the swipeview
//Add a swip control to the and have it go all the way across. See if this works
// SwipeItems
SwipeItem tobj_DeleteSwipeItem = new SwipeItem
{
Text = "Delete",
BackgroundColor = Color.LightGreen
};
tobj_DeleteSwipeItem.Invoked = OnDeleteSwipeItemInvoked; ;
List<SwipeItem> tobj_swipeItems = new List<SwipeItem>() { tobj_DeleteSwipeItem }; //comment out
//SwipeItems tobj_swipeItems = new SwipeItems();
//tobj_swipeItems.Add(tobj_DeleteSwipeItem);
// SwipeView content
Label swipeContent = new Label()
{
BackgroundColor = Color.Pink,
Text = "test",
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
var tobj_SwipeGestureRecognizer = new SwipeGestureRecognizer() { Direction = SwipeDirection.Right };
tobj_SwipeGestureRecognizer.Swiped = tobj_SwipeGestureRecognizer_Swiped;
swipeContent.GestureRecognizers.Add(tobj_SwipeGestureRecognizer);
SwipeView swipeView = new SwipeView
{
LeftItems = new SwipeItems(tobj_swipeItems) { Mode = SwipeMode.Reveal },
Content = swipeContent
};
//Add the swipeview to the grid
grdDataGrid.Children.AddVertical(swipeView);
// grdDataGrid.Children.Add(swipeView, 0, pi_RowNumber);
// Grid.SetColumnSpan(swipeView, grdDataGrid.ColumnDefinitions.Count);
}
catch (Exception ex)
{
// SharedErrorHandler.ProcessException(ex);
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
private void tobj_SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
{
try
{
if (e.Direction == SwipeDirection.Right)
{
var tobj_SwipeView = ((Label)sender).Parent as SwipeView;
tobj_SwipeView.Open(OpenSwipeItem.LeftItems);
}
}
catch (Exception ex)
{
//SharedErrorHandler.ProcessException(ex);
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
筆記:
When adding the SwipeControl to Grid, I used code:
grdDataGrid.Children.AddVertical(swipeView);
And to simplify the code,I removed the parameter of function AddSwipeControl().
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442276.html
