Как изменить размер шрифта списка в xamarin? - PullRequest
0 голосов
/ 24 января 2020

Это мой код в файле edit.cs

            var db = new SQLiteConnection(_dbPath);

            StackLayout stackLayout = new StackLayout();

            _listView = new ListView();
            _listView.ItemsSource = db.Table<SpeechRecTable>().OrderBy(x => x.Text).ToList();
            _listView.ItemSelected += _listView_ItemSelected;
            //_listView.SeparatorColor = Color.WhiteSmoke;
            stackLayout.Children.Add(_listView);

            _button = new Button();
            _button.Text = "UPDATE";
            _button.BackgroundColor = Color.Coral;
            _button.TextColor = Color.WhiteSmoke;
            _button.Clicked += _button_Clicked;
            stackLayout.Children.Add(_button);

            Content = stackLayout;

Я новичок в xamarin, и я пытаюсь создать приложение CRUD, я следую этому руководству: https://www.youtube.com/watch?v=aabHAgY5VXo&t=58s

Кажется, я не могу настроить размер шрифта, это просто файл .cs, а не файл xaml.cs

Ответы [ 2 ]

0 голосов
/ 24 января 2020

Попробуйте изменить ваш код следующим образом:

    var db = new SQLiteConnection(_dbPath);

    StackLayout stackLayout = new StackLayout();

    ListView _listView = new ListView
    {  
        // template for displaying each item.
        ItemTemplate = new DataTemplate(() =>
        {
            Label nameLabel = new Label();
            nameLabel.TextColor = Color.Black;
            nameLabel.FontSize = 15;
            nameLabel.SetBinding(Label.TextProperty, "Name");

            return new ViewCell
            {
                View = new StackLayout
                {
                    Padding = new Thickness(5),
                    VerticalOptions = LayoutOptions.Center,
                    Children = 
                    {
                        nameLabel
                    }
                }
            };
        })
    };

    //_listView.SeparatorColor = Color.WhiteSmoke;
    _listView.ItemsSource = db.Table<SpeechRecTable>().OrderBy(x => x.Text).ToList();
    _listView.ItemSelected += _listView_ItemSelected;
    stackLayout.Children.Add(_listView);

    _button = new Button();
    _button.Text = "UPDATE";
    _button.BackgroundColor = Color.Coral;
    _button.TextColor = Color.WhiteSmoke;
    _button.Clicked += _button_Clicked;
    stackLayout.Children.Add(_button);

    Content = stackLayout;
0 голосов
/ 24 января 2020

Я покажу вам 2 решения.

В C#

public class CustomCell : ViewCell
{
    public CustomCell()
    {
        var nameLabel = new Label();
        var verticaLayout = new StackLayout();
        var horizontalLayout = new StackLayout();

        //set bindings
        nameLabel.SetBinding(Label.TextProperty, new Binding("Name"));
        nameLabel.FontSize = 24;

        //add views to the view hierarchy
        verticaLayout.Children.Add(nameLabel);
        horizontalLayout.Children.Add(verticaLayout);

        // add to parent view
        View = horizontalLayout;
    }
}

_listView.ItemTemplate = new DataTemplate(typeof(CustomCell));

или в Xaml

 <StackLayout>
        <ListView x:Name="ItemsListView"
                ItemsSource="{Binding Items}"
                VerticalOptions="FillAndExpand"
                HasUnevenRows="true"
                RefreshCommand="{Binding LoadItemsCommand}"
                IsPullToRefreshEnabled="true"
                IsRefreshing="{Binding IsBusy, Mode=OneWay}"
                CachingStrategy="RecycleElement"
                ItemSelected="OnItemSelected">
            <d:ListView.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>First Item</x:String>
                    <x:String>Second Item</x:String>
                    <x:String>Third Item</x:String>
                    <x:String>Fourth Item</x:String>
                    <x:String>Fifth Item</x:String>
                    <x:String>Sixth Item</x:String>
                </x:Array>
            </d:ListView.ItemsSource>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="10">
                            <Label Text="{Binding Text}" 
                                d:Text="{Binding .}"
                                LineBreakMode="NoWrap" 
                                Style="{DynamicResource ListItemTextStyle}" 
                                FontSize="16" />
                            <Label Text="{Binding Description}" 
                                d:Text="Item descripton"
                                LineBreakMode="NoWrap"
                                Style="{DynamicResource ListItemDetailTextStyle}"
                                FontSize="13" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...