Код: Выделить всё
lightGrey
#1f1f1f
Код: Выделить всё
class CreateDynamicTab(string tabName, UraniumUI.Material.Controls.TabView tabView)
{
string tabHeaderName = tabName;
UraniumUI.Material.Controls.TabView sourceTabView = tabView;
public TabItem CreateNewTab()
{
var testList = new List();
testList.Add(("variabl 1 ", "variable 2 ", 3));
testList.Add(("variabl 4 ", "variable 5 ", 6));
testList.Add(("variabl 7 ", "variable 8" , 9));
var resultMessage = new VerticalStackLayout
{
Spacing = 10,
Padding = 10
};
foreach (var item in testList)
{
resultMessage.Children.Add
(
new Label { Text = item.var1 + item.var2 + item.value }
);
}
var newTab = new TabItem
{
Title = "2nd Tab",
Content = resultMessage
};
newTab.HeaderTemplate = new DataTemplate(() =>
{
//Create main container for header elements of tab.
var headerLayout = new Microsoft.Maui.Controls.Grid
{
ColumnDefinitions =
{
new Microsoft.Maui.Controls.ColumnDefinition{ Width = new GridLength(1, GridUnitType.Star ) },
new Microsoft.Maui.Controls.ColumnDefinition { Width = GridLength.Auto}
}
};
//Form the Text element displayed on tab (The Label)
var titleLabel = new Label
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Start,
Padding = 5
};
titleLabel.Text = tabHeaderName;
//Form the Button Element on the tab for closing itself.
var closeButton = new Microsoft.Maui.Controls.Button
{
Text = "X",
HeightRequest = 25,
WidthRequest = 26,
Padding=1,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.End
};
closeButton.Clicked += (s, e) =>
{
sourceTabView.Tabs.Remove(newTab);
};
//Add layout and button to the header element of the tab.
headerLayout.Add(titleLabel);
headerLayout.Add(closeButton);
return headerLayout;
});
return newTab;
}
}
Код: Выделить всё
public void CreateNewTabViewElement()
{
var newTab = new CreateDynamicTab("Test Tab", TabViewElement);
var createdTab = newTab .CreateNewTab();
TabViewElement.Tabs.Add(createdTab );
}
На всякий случай и для ответа на любые вопросы вот дополнительная информация и визуальное представление проблемы:
Раздел XAML TabView:
Код: Выделить всё
