我在使用Xamarin在我的移动应用程序中显示图像时遇到了一些麻烦 . 当我使用Image放入图像时,它工作正常,但是当我尝试设置与titleIcon相同的图像或将图像放在按钮上时,它不起作用 . 这些案件有一些根本的区别吗?
相关设置:
public partial class ThisPage : ContentPage
{
...
public ThisPage(string data)
{
Content = new ScrollView()
{
Orientation = ScrollOrientation.Vertical,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Margin = new Thickness(10, 0)
};
...
(Content as ScrollView).Content = new StackLayout()
{
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
...
StackLayout layout = (Content as ScrollView).Content as StackLayout;
工作得很好
Title = titleString;
不行!
NavigationPage.SetTitleIcon(this as ContentPage, filename);
工作正常,显示我的titleIcon作为图像完美地工作
Image im = new Image
{
Source = filename,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
layout.Children.Add(im);
至于按钮......
AddButton(buttonData, layout);
...
}
public void AddButton(string buttonData, StackLayout layout)
{
...
Button b = new Button
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
//CornerRadius = 14 //Does not compile
BorderRadius = 14 //Gives a warning telling me that I should use CornerRadius instead
};
这很好用
b.Text = buttonText;
这根本不起作用 .
b.Image = filename;
该按钮始终显示在页面上,它上面只有一个图像 . 手动更大按钮和/或省略文本也不会导致图像出现 .
layout.Children.Add(b);
}
}
更多数据:
在运行Android版本6.0.1的DuraForce Pro上使用Xamarin Live应用程序进行测试 .
Visual Studio Community 2015中的“运行”按钮显示“KYOCERA KYOCERA-E6820播放器(Android 6.0 - API23)” .
安装到项目中的Xamarin.Forms版本(NuGet)是最新稳定版本2.5.0.280555
编辑:
感谢下面的DennisSchröer,图像现在可以正常使用按钮,但它们仍然不能作为 Headers 图标 .
我试图用于 Headers 图标的图像是一个80 x 578像素的jpg .
它在Android子项目的Resources / drawable文件夹中作为AndroidResource,而iOS子项目的Resources文件夹作为BundleResource .
我还无法测试应用程序的iOS版本,因为我最近无法更新到最新版本的XCode . (最新版本与之前连接我的PC以编译iOS版本的Mac不兼容) . 因此,我现在只调试Android版本 .
我也尝试直接在页面的xaml文件中设置属性,但无济于事:
<?xml version="1.0 encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApplicationName.Views/ThisPage"
NavigationPage.TitleIcon="Logo.jpg"
</ContentPage>
我也尝试将它作为ImageView直接添加到Toolbar.axml中,但这也不成功:
<?xml version="1.0" encoding="UTF-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/Logo.jpg"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
对于任何想要根据DennisSchröer的建议看看我做出什么改变的人来说,这是新的AddButton:
public void AddButton(String current, StackLayout layout)
{
string[] segments = current.Split('|');
string link = null;
Button b = null;
Image im = null;
int i = 0;
foreach (string segment in segments)
{
switch (i)
{
case 0:
{
link = segment;
}
break;
case 1:
{
if (segment.Length > 0)
{
b = new Button
{
Text = segment,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
//CornerRadius = 14 //Does not compile: "'Button' does not contain a definition for 'CornerRadius'
BorderRadius = 14 //Gives a warning: "'Button.BorderRadius' is obsolete: 'BorderRadius is obsolete as of 2.5.0. Please use CornerRadius instead.'"
};
}
}
break;
case 2:
{
im = new Image
{
Source = segment,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand
};
}
break;
}
i++;
}
if (im != null)
{
TapGestureRecognizer tap = new TapGestureRecognizer();
if ((link.StartsWith(c_linkType)) || (link.StartsWith(c_mailType)))
{
tap.Tapped += (o, e) =>
{
LinkClicked(link);
};
}
else
{
tap.Tapped += (o, e) =>
{
ButtonClicked(link);
};
}
im.GestureRecognizers.Add(tap);
layout.Children.Add(im);
}
if (b != null)
{
if ((link.StartsWith(c_linkType)) || (link.StartsWith(c_mailType)))
{
b.BackgroundColor = Color.White;
b.TextColor = Color.Blue;
b.BorderColor = Color.White;
b.BorderWidth = 0;
b.Clicked += (o, e) =>
{
LinkClicked(link);
};
}
else
{
b.BackgroundColor = new Color(186.0 / 256.0, 39.0 / 256.0, 45.0 / 256.0);
b.TextColor = Color.White;
b.Clicked += (o, e) =>
{
ButtonClicked(link);
};
}
layout.Children.Add(b);
}
}
1 回答
不要添加
.jpg
,请使用此:或者您可以将
android:id="@+id/toolbarImage"
添加到ImageView
:在你的
MainActivity
中添加以下LoadApplication(new App());
: