我Resources.GetDrawable在andrdoid Xamarin中实现了.该程序有效,但如果我点击按钮实现Resources.GetDrawable程序强制关闭.这是我的代码:
SetContentView(Resource.Layout.Main);
drawView = FindViewById<DrawingView>(Resource.Id.drawing);
LinearLayout paintLayout = FindViewById<LinearLayout>(Resource.Id.paint_colors);
currPaint = (ImageButton)paintLayout.GetChildAt(0);
currPaint.SetImageDrawable(Resources.GetDrawable(Resource.Drawable.paint_pressed));
Run Code Online (Sandbox Code Playgroud)
在Resources.GetDrawable(Resource.Drawable.paint_pressed得到绿色下划线(在Visual Studio 2015年Xamarin).日志消息返回异常:
Android.Content.Res.Resources + NotFoundException
'Resources.GetDrawable(int)'已过时:'deprecated'
对于Java版本,解决方案是使用以下三种方法之一:
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
//or
ContextCompat.getDrawable(getActivity(), R.drawable.name);
//or
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
Run Code Online (Sandbox Code Playgroud)
C#Android Xamarin的版本是什么?谢谢.
在 http.Client 中设置的超时和在请求上下文中设置的超时有什么区别?
我见过 2 种在 http 客户端设置超时的方法。
第一的:
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
Run Code Online (Sandbox Code Playgroud)
第二:
client := http.Client{
Timeout: 2 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
Run Code Online (Sandbox Code Playgroud)
什么时候使用一个?
如何获取上下文中超时设置的持续时间。
例子:
func f(ctx context.Context) {
// get ctx timeout value
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(),2*time.Second)
defer cancel()
f(ctx)
}
Run Code Online (Sandbox Code Playgroud)
如何2*time.Second从函数内获取持续时间f?
假设我有这个 json 字符串:
{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\",\"D\":2,\"E\":\"e\"}
Run Code Online (Sandbox Code Playgroud)
我想将上面的字符串转换为结构:
{
A string
B string
C string
D int
E string
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何做到这一点,因为我已经进行了引用和取消引用,但似乎还没有成功。
我一直在努力解决我在Golang并发中遇到的这个简单问题.我一直在寻找所有可能的解决方案,但没有发现我的问题(或者我可能会错过一个).这是我的代码:
package main
import (
"fmt"
"time"
)
func producer(ch chan int, d time.Duration, num int) {
for i:=0; i<num; i++ {
ch <- i
time.Sleep(d)
}
}
func main() {
ch := make(chan int)
go producer(ch, 100*time.Millisecond, 2)
go producer(ch, 200*time.Millisecond, 5)
for {
fmt.Println(<-ch)
}
close(ch)
}
Run Code Online (Sandbox Code Playgroud)
它打印错误:
致命错误:所有goroutines都睡着了 - 僵局!
goroutine 1 [chan receive]:main.main()D:/Code/go/src/testconcurrency/main.go:23 + 0xca退出状态2
什么是避免此错误的有效方法?,谢谢.
在 Go 中,为什么这段代码可以工作:
package main
import (
"fmt"
)
func main() {
a := []int{1}
a = a[1:]
fmt.Println(len(a))
}
Run Code Online (Sandbox Code Playgroud)
但这并没有:
package main
import (
"fmt"
)
func main() {
a := []int{1}
a = a[2:]
fmt.Println(len(a))
}
Run Code Online (Sandbox Code Playgroud)
我听说过切片容量,有人可以详细说明吗?
我做了一个服务器,客户端通过 http 进行访问。我在客户端的 Transport 的 RoundTripper 方法中设置了重试机制。以下是每个服务器和客户端的工作代码示例:
服务器主.go
package main
import (
"fmt"
"net/http"
"time"
)
func test(w http.ResponseWriter, req *http.Request) {
time.Sleep(2 * time.Second)
fmt.Fprintf(w, "hello\n")
}
func main() {
http.HandleFunc("/test", test)
http.ListenAndServe(":8090", nil)
}
Run Code Online (Sandbox Code Playgroud)
客户端main.go
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
)
type Retry struct {
nums int
transport http.RoundTripper
}
// to retry
func (r *Retry) RoundTrip(req *http.Request) (resp *http.Response, err error) {
for i := 0; i < r.nums; i++ {
log.Println("Attempt: ", …Run Code Online (Sandbox Code Playgroud) 在 Golang SQL 包中有 QueryRow 和 Exec 用于执行查询。如果我在事务中执行插入查询,在性能方面使用哪个更好?
err = tx.QueryRow(query, params).Scan(&id)
对比
result, err = tx.Exec(query, params)
我想利用 golang 中的上下文在超时时用于取消。
代码:
package main
import "fmt"
import "time"
import "context"
func F(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx,3*time.Second)
defer cancel()
for i:=0;i<10;i++ {
time.Sleep(1 * time.Second)
fmt.Println("No: ",i)
}
select {
case <-ctx.Done():
fmt.Println("TIME OUT")
cancel()
return ctx.Err()
default:
fmt.Println("ALL DONE")
return nil
}
}
func main() {
ctx := context.Background()
err := F(ctx)
if err != nil {
fmt.Println(err)
}else {
fmt.Println("Success")
}
}
Run Code Online (Sandbox Code Playgroud)
期望:上面的代码应该在 counter 处停止运行循环2,因为超时为 3 秒,循环每次运行 1 秒。所以我期待这样的事情:
No: 0
No: …Run Code Online (Sandbox Code Playgroud) 返回 JSON 结果时,使用 marshalledstruct或map. 我见过一些代码使用 map 并将它们编组为 json 返回,其他代码使用编组结构返回 json 响应。
例子:
使用struct:
type Response struct {
A int `json:"a"`
B string `json:"b"`
}
r := Response{A:1,B:"1"}
resp, _ := json.Marshal(r)
Run Code Online (Sandbox Code Playgroud)
使用map:
m := map[string]interface{}{
A: 1,
B: "1",
}
resp, _ := json.Marshal(m)
Run Code Online (Sandbox Code Playgroud)
哪个更好?
我知道 Golang 包含本机内置 Web 服务器(net/http),可以用作服务器而无需使用外部 Web 服务器(apache、nginx 等)。对于本地开发,您只需运行即可http.ListenAndServe,您的服务器已在本地准备就绪。
我的问题是,如何设置您的 golang 应用程序以供其他人公开访问,而不需要外部 Web 服务器?